3 Replies - 254 Views - Last Post: 03 October 2012 - 05:49 PM Rate Topic: -----

#1 Javano0b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-October 12

Time class confusion!

Posted 02 October 2012 - 06:53 PM

So I really have no idea how to go about this assignment for my AP Computer Science class in high school. I have to take an incomplete definition of the Time class, and do all this crazy stuff with it. I've been trying for a couple days but my deadline is coming up soon and I'm really hoping someone can help me out.

Consider the following incomplete definition of the Time class, used to represent a time of day:

public class Time
{
private int hour; //a value form 0 to 23
private int minutes; //a value from 0 to 59
public Time()
{
}
public Time(int newHour, intNewMinutes)
{
}
}



Questions:

1) Complete the constructors as defined

2) Add a printTime method that returns a string representing the time in a 12-hour formate, including am or pm as appropriate. Midnight, when both the hour and minutes fields are zero, and noon, when the hour field is 12 and the minutes field is zero, should be treated as special cases; instead of returning the time using numbers, the words midnight or noon should be returned.

3) Add an add10 method that adds 10 minutes to the current time.

4) Add a subtract10 method that subtracts 10 minutes from the current time.

5) Test the Time class with a client class
---- Create two Time objects and enter a loop that does the following
---------- print current time for each object
---------- ask if the user would like to add time to the first object and then update accordingly
---------- ask if the user would like to add time to the second time object and then update accordingly
---------- ask if the user would like to subtract time from the first time object then update accordingly
---------- you can guess what this is
---------- repeat the loop until the user is ready to quit testing
-- each and every method in the Time class must be called at least once within the client class.

I haven't started the test class yet, but this is what I have for the first four questions, let me know if I'm on the right track or if I'm doing anything wrong! Thanks in advance.

import cs1.Keyboard;

public class Time
{
	private int hour; //a value from 0 to 23
	private int minutes; //a value from 0 to 59

	public Time()
	{
		public static String printTime(String num)
		{
			int Time = 0;
			Time timeClass = new Time (11, 30); //parameters are hour, minutes
			if (Time = 0, 0);
				System.out.print("midnight");
			if (Time = 12, 0);
				System.out.print("noon");
				else
					return Time;
		}

		public void addTen(int num1)
		{
			minutes = minutes+10; //add ten minutes
		}

		public void subtractTen(int num2)
		{
			minutes = minutes-10; //subtract ten minutes
		}
	}
	public Time(int newHour, int newMinutes)
	{
		hour = newHour();
		minutes = newMinutes();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Time class confusion!

#2 dimasalang  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 02-October 12

Re: Time class confusion!

Posted 03 October 2012 - 12:51 AM

Hi. I think it would throw an error. It's better to define your printTime() method outside your constructor. Also, define setter and getter for your hour and minutes variables. :)
Was This Post Helpful? 1
  • +
  • -

#3 Javano0b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-October 12

Re: Time class confusion!

Posted 03 October 2012 - 07:40 AM

Ok I have the first four questions predominantly figured out now, thanks. How does this look for the client (test) class?

import cs1.Keyboard;

public class TestTime
{
	public static boolean main (String[] args)
	{
		Object t1 = new Object();
		Object t2 = new Object();
		Boolean b = new Boolean(true);
	}

	while (b = true);
	{
	System.out.println(Time);
	System.out.println("Would you like to add ten seconds to the time?");
		Keyboard.readBoolean();
	}
	if(b = true);
		Time.addTen();
		System.out.println("It is now" + Time.addTen);
	}
		else
	{
			System.out.println("The time is" + Time);
	}
	System.out.println("Would you like to subtract ten seconds from the time?");
		Keyboard.readBoolean();
		{
	if(b = true);
		Time.subtractTen();
		System.out.println("It is now" + Time.subtractTen);
		}
		{
		else
			System.out.println("The time is" + Time);
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 dimasalang  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 02-October 12

Re: Time class confusion!

Posted 03 October 2012 - 05:49 PM

Hi. I don't know if that will work in terms of testing your Time class. But I guess it should be something like this
instantiate Time using Time(hour, minute) as t

// or get input from user for hour and minute
instantiate Time using Time() as t

Ask minute from user
t.setMinute(minute given by user)

Ask hour from user
t.setHour(hour given by user)

Ask user if he wants to add ten minutes
if yes call the addTen method
if no do nothing

Ask user if he wants to subtract ten minutes
if yes call the subtractTen method
if no do nothing


Code wise, you have some syntax errors. Like this one, your while loop is outside your main method. I think it won't work. youd should place it inside the main method(Lines from 12 to 36). And there are some ';' that may cause errors like the ones in Line 18 and 29. And some missing curly braces.
I have tried to correct it and come up with something like this:
import cs1.Keyboard;

public class TestTime
{
	public static boolean main (String[] args)
	{
		Object t1 = new Object();
		Object t2 = new Object();
		Boolean b = new Boolean(true);
		while (b = true){
		System.out.println(Time);
		System.out.println("Would you like to add ten seconds to the time?");
			Keyboard.readBoolean();
		}
		if(b = true){//;
			Time.addTen();
			System.out.println("It is now" + Time.addTen);
		}else{
			System.out.println("The time is" + Time);
		}
		System.out.println("Would you like to subtract ten seconds from the time?");
		Keyboard.readBoolean();
			
		if(b = true){//;
			Time.subtractTen();
			System.out.println("It is now" + Time.subtractTen);
		}
		//{
		else{
				System.out.println("The time is" + Time);
		}
	}

	
}

:bigsmile: hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1