aspose file tools*
The moose likes Beginning Java and the fly likes classes and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classes and methods" Watch "classes and methods" New topic
Author

classes and methods

Rich Nello
Greenhorn

Joined: Dec 08, 2003
Posts: 16
Hi All.
I'm very new to Java (or rather, to programming in general). My first year university requires me to learn Java and OOP. I'm having problems (as U can imagine) with calling methods from different classes.
For example, I created a class classed 'Clock' (this contains three variables: hours, minutes and seconds). I have 2 constructors as shown below:
// 1st constructor
public Clock ()
{
}

//2nd constructor
public Clock (int hour, int minute, int second)
{
hour =0;
minute =0;
second =0;
}
I need to create two methods within this class for UK Time and US time differences. What I would like is for these methods to simply calculate the hour(s) difference between the time inputted by the user and the equivalent in the US and UK.
The user will be inputting the hour, minute and second in another file called ClockTest.java. And that's where I'm stuck!!
How to create the methods??? But more than that ... how (as a greenhorn) can I get the user to input values for hour, min and sec and then store those values in a new object (of the class Clock)called 'watchOne'. Even explaining it is making me dizzy!!!
Hope you can help me!!


Rich Nello (Java Greenhorn)
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Wow...quite a few questions! OK. So it sounds like you want the user to enter in the time. How do you plan on doing this? Are you going to use a GUI (a label, a text field, and a button)? Or do you want to just test this program by entering the arguments when you run the program (java Time.class hh, mm, ss )? You answer will obviously change our help for you. This is actually a very straightforward program, and will be very simple once you figure out how you want to implement it.


David Crossett
-nothing important to say, but learnin' plenty-
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Rich Nello:
how (as a greenhorn) can I get the user to input values for hour, min and sec and then store those values in a new object (of the class Clock)called 'watchOne'.

A lot of kids now-a-days use that newfangled SimpleIO to streamline their IO routines. Depending on your ability to follow instructions, it may or may not be an option for you, since you have to compile it before using it.
At it's simplest, IO can be accomplished through the java.lang.System class. System.out.println("hello") writes a String, here the literal String "hello", to the console.
System.in.read() will read in a single character as an int. If you are really ambitious you can wrap System.in with a BufferedReader to read in a String at a time.


[How To Ask Questions On JavaRanch]
Rich Nello
Greenhorn

Joined: Dec 08, 2003
Posts: 16
Hey guys, thanks a million for the hints. I really appreciate it! I was thinking that maybe it would better if I just showed you the code. That way you can see for yourself. I'm adding the output at the end ... so you can also see where my problem is.
(Thanks for time ))
==============================================================
==============================================================
/*
*Clock.java
*/
import java.io.*;
public class Clock
{
//class variables
int hour;
int minute;
int second;

//1st constructor
public Clock ()
{
}


//2nd constructor
public Clock (int hour, int minute, int second)
{
hour =0;
minute =0;
second =0;
}


//method for this class (To Calculate the equivalent in UK Time)
double difference() //equivalent UK Time
{
return (this.hour) - 1;
}
}

==============================================================
==============================================================

/*
*ClockTest.java
*/
import java.io.*;
public class ClockTest
{
public static void main(String args[]) throws IOException
{
BufferedReader key =new BufferedReader (new InputStreamReader (System.in));

//declare all needed variables for this class
int h;//hour
int m;//minute
int s;//second
String user;//user input variable
Clock firstWatch;//first object of type Clock
Clock secondWatch;//second object of type Clock


//create new object of type Class named firstWatch
firstWatch = new Clock();

//print firstWatch details to screen with NO USER INPUT
System.out.println(firstWatch);
NewLine.newLine();//method in NewLine.java
System.out.println(firstWatch.hour);
System.out.println(firstWatch.minute);
System.out.println(firstWatch.second);
NewLine.newLine();

/*
//create new object of type Class named secondWatch
secondWatch =new Clock();
*/

//print on-Screen Instructions for the users
System.out.println("You are now required to key in some values. Values required are listed below:");
System.out.println("- Hour");
System.out.println("- Minute");
System.out.println("- Second");
NewLine.newLine();//method in NewLine.java

//user will key in these values fro secondWatch
System.out.println("Please key in a value for HOURS ...");
user =key.readLine();
h =Integer.parseInt(user);//hour
NewLine.newLine();

System.out.println("Now key in a value for MINUTES ...");
user =key.readLine();
m =Integer.parseInt(user);//minute
NewLine.newLine();

System.out.println("And finally, please key in a value for SECONDS ...");
user =key.readLine();
s =Integer.parseInt(user);//second
NewLine.newLine();
NewLine.newLine();

//create new object of type Class named secondWatch
secondWatch =new Clock(h, m, s);

//after user keys in all three required values the reult screen appears
System.out.println("Thank You!");
System.out.println("===========");

//print secondWatch details to screen WITH USER INPUT
System.out.println("This is what you typed in for the secondWatch");
System.out.println("==============================================");
NewLine.newLine();
System.out.println(secondWatch.hour);
System.out.println(secondWatch.minute);
System.out.println(secondWatch.second);
System.out.println(secondWatch.difference());
NewLine.newLine();


System.out.println("THE END >>> Hit the ENTER KEY to end this program");
NewLine.newLine();
try
{
System.in.read();
}

catch (IOException x)
{
return;
}
}
}

==============================================================
==============================================================
>>>>>>> THIS IS MY OUTPUT <<<<<<<<
==============================================================
==============================================================

Clock@bd0108
0
0
0
You are now required to key in some values. Values required are listed below:
- Hour
- Minute
- Second
Please key in a value for HOURS ...
12
Now key in a value for MINUTES ...
30
And finally, please key in a value for SECONDS ...
55

Thank You!
===========
This is what you typed in for the secondWatch
==============================================
0
0
0
-1.0
THE END >>> Hit the ENTER KEY to end this program

Press any key to continue...
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Rich Nello:
so you can also see where my problem is.

Please use the UBB Code "CODE" tag to preserve your code formatting.
Looks like it runs. What's the problem?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One thing that jumped out at me ... This probably isn't what you wanted to do:

More likely you wanted to save the values coming into the constructor.

Does that help?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Dang. That took me too long to figure out. The problem is that on your clock constructor, you are initializing h, m, and s to 0 no matter what values are passed to you. You need to change from

That should fix it. Also, instead of accessing Clock's variables directly, you should have getHour(), getMinute(), and getSecond() methods.
Rich Nello
Greenhorn

Joined: Dec 08, 2003
Posts: 16
That was it!!! Cheers David. SPOT ON!!!
public Clock (int hour, int minute, int second)
{
this.hour =hour;
this.minute =minute;
this.second =second;
}
******************************************************

I tried this too as suggested by Stan:
code:
-------------------------------------------------------
h = hour;
m = minute;
s = second;
However, that failed since I didn't have h, m and s declared as variables at the beginning of my constructor.
-------------------------------------------------------
Now, all's working well!! The sematics for my ClockTest.java are:
1>>>>>
//declare all needed variables for this class
int h;//hour
int m;//minute
int s;//second
2>>>>>
populate h, m and s as parameters in the new Object 'seconWatch'
//create new object of type Class named secondWatch
secondWatch =new Clock(h, m, s);
3>>>>>
My println statements all work fine!!
System.out.println(secondWatch.hour);
System.out.println(secondWatch.minute);
System.out.println(secondWatch.second);
System.out.println(secondWatch.difference());

Thanks a lot more than a million!! You guys are so great.
narakom!! (in my language, this means 'See You Soon')
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classes and methods