Writing Classes

K

KyoGaSuki

(I should start making an ebook based on all of the help you have
given me so far, thank you all) So -- classes...oh joy...joy...I was
just having trouble with methods...now classes? I look at
examples...I look at them until my brain hurts...I just can't seem to
follow them far. For example, I was looking at an example about a
clock (where you go in and set it, ect)...but then the variable "min"
became "minutes" and then it became "min" again, and I think
eventually it was "min = temp.min" ...I was already confused prior to
that, but that just finished me off ...that, and the comments
definitely didn't help...*defeated* eh, why not -- I guess I will
post the example here anyways. I have a complete arsenal of ebooks on
JAVA at my disposal...but there is so many of them...does anyone have
any suggestions to which explains writing classes the easiest?

example:

/**
* @(#)Clock.java
*
* Clock application
*
* @author
* @version 1.00 2008/4/1
*/

public class Clock {

private int hr; //store hours
private int min; //store min
private int sec; //store sec

//Default constructor
//Postcondition: hr = 0; min = 0; sec = 0
public Clock() {
setTime(0,0,0);
}
//Constructor with parameters, to set the time
//The time is set according t the parameters
//Postcondition: hr = hours; min = minutes; sec = seconds
public Clock(int hours, int minutes, int seconds) {
setTime(hours,minutes, seconds);
}
//Method to set the time
//the time is set according to the parameteres
//Postcondition: hr = hours; min = minutes; sec = seconds
public void setTime(int hours, int minutes, int seconds) {
if(0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if(0 <=minutes && minutes < 60)
min = minutes;
else
min = 0;
if(0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
//Method to return the hours
//Postcondition: The value of hr is returned
public int getHours(){
return hr;
}
//Method to return the minutes
//Postcondition: The value of min is returned.
public int getMinutes() {
return min;
}
//Method to return the seconds
//Postcondition: The value of sec is returned.
public int getSeconds() {
return sec;
}
//Method to print the time
//Postcondition: Time is printed in the form hh:mm:ss
public void printTime() {
if(hr<10)
System.out.print("0");
System.out.print(hr + ":");
if(min < 10)
System.out.print("0");
System.out.print(min + ":");
if(sec <10)
System.out.print("0");
System.out.print(sec);
}
//Method to increment the time by one second
//Postcondition: The time is incremented by one second
//If the before-increment time is 23:59:59, the time
//is reset to 00:00:00.
public void incrementSeconds() {
sec++;
if (sec>59)
{
sec = 0;
incrementMinutes(); //increment minutes
}

}
//Method to increment the time by one minute
//Postcondidtion: The time is incremented by one minute.
//If the before-increment time is 23:59:53, the time
//is reset to 00:00:53.
public void incrementMinutes() {
min++;

if(min>59)
{
min=0;
incrementHours(); //increment hours
}
}
//Method to increment the time by one hour
//Postcondition: The time is incremented by one hour.
//If the before-increment time is 23:45:53, the time
//is reset to 00:45:53.
public void incrementHours() {
hr++;

if(hr>23)
hr=0;
}
//Method to compare the two times
//Postcondition: Returns true if time is equal to
// otherClock; otherwise returns false.
public boolean equals(Clock otherClock) {
return (hr==otherClock.hr
&&min == otherClock.min
&&sec == otherClock.sec);
}
//Method to copy the time
//Postcondition: The instance variables of otherClock are
// copied into the corresponding data members
// of this time.
// hr = otherClock.hr; min = otherClock.min;
// sec = otherClock.sec.
public void makeCopy(Clock otherClock){
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
//Method to return a copy of the time
//Postcondition: A copy of the object is created
// and a reference of the copy is returned.
public Clock getCopy() {
Clock temp= new Clock();

temp.hr = hr;
temp.min = min;
temp.sec = sec;

return temp;
}
}
 
K

KyoGaSuki

that, and everywhere I look in classes in general they have the most
random names that they seem to pull out of nowhere. and I never find
anything else like it through the rest of the code. kinda like:
RANDOM-WORD.day
 
K

KyoGaSuki

KyoGaSuki said:
...does anyone have
any suggestions to which explains writing classes the easiest?

I think it's easiest to explain how a class is a blueprint for an
object. Maybe starting with class diagrams instead of code is also
easier to understand, since it's a visual representation (and introduces
students to UML and a structured approach to design).

+-------+      +-----------+
| clock | ---- |wrist watch|
+-------+      +-----------+
|minutes|      
| etc   |      +-----------------+
+-------+ ---- |grandfather clock|
|set()  |      +-----------------+
+-------+

or similar. If you look up UML [1] you'll understand.

[1] <http://www.uml.org>

Thank you ^^ I'll look it up.
 
K

KyoGaSuki

this was an example from the book they are asking us to learn from
x.x maybe that is why I don't seem to understand it? I am so new to
classes that all I really understand about them is:

public class Clock {

private int hr;
private int min;
private int sec;
}

basically, you name the class, curly braces, define variables. i work
best when shown a basic example, and then i can work from that... fate
have it that i was absent the day he showed examples x.x
 
C

Chase Preuninger

min and minutes are two separate variables. one is declared locally
in a method and one is an instance field of the class.
and temp.min is simply calling the min variable in the Clock object
stored in temp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top