Java programming (Time class) question – cannot find symbol?
Java programming (Time class) question – cannot find symbol?
In my java class we are supposed to code a time class that takes an amount of hours and minutes and converts it into a AM/PM time (im pretty sure anyways). This is what i have:
// Represents the time of the day in hours
// and minutes using the 24-hour clock.
public class Time
{
private int hours;
private int minutes;
public Time(int h,int m)
{
if ((h<24&&h>0) && (m<60&&m>0))
{
hours = h;
minutes = m;
}
else
{
throw new IllegalArgumentException(“INVALID”);
}
}
//This method returns the time in minutes
private int toMins()
{
return (60&hours)+minutes;
}
//Returns true if this time is earlier,
// false if a time passed as an argument or the times are the same
public boolean lessThan(Time t)
{
return toMins()
public String toString()
{
if(hours>12)
{
return hours+”:”+minutes+”P.M”;
}
else
{
return hours+”:”+minutes+”A.M”;
}
}
// Calculates the number of minutes that have passed by
// between the argument Time t and this time object.
public int elapsedSince(Time t)
{
if(t.toMins()
else
return (60*24)-(t.toMins()-toMins());
}
}
then to test that code we were given a TestTime code:
import java.util.Scanner;
public class TestTime
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Time 1" );
System.out.print("Enter hours: " );
int h1 = keyboard.nextInt();
System.out.print("Enter mins: " );
int m1 = keyboard.nextInt();
Time t1 = new Time(h1, m1);
System.out.println("Time 2" );
System.out.print("Enter hours: " );
int h2 = keyboard.nextInt();
System.out.print("Enter mins: " );
int m2 = keyboard.nextInt();
Time t2 = new Time(h2, m2);
System.out.println(t1 + " <= " + t2 + "? " + t1.lessThan(t2));
System.out.println(t2.elapsedSince(t1) + " minutes elapsed from t1 to t2.");
}
}
The problem is that when i compile the TestTime code, I get four errors all saying cannot find symbol pointing at these lines of TestTime:
Time t1 = new Time(h1, m1);
Time t2 = new Time(h2, m2);
Can anyone help me?
Thank you!!
Best answer:
Your code runs for me… It’s got to be a problem with your packages, as the javac can’t find the class Time. If you put TestTime as an inner static class of Time, everything runs nicely. Try this:
package com.jjk.util;
import java.util.Scanner;
public class Test
{
private int hours;
private int minutes;
public Test(int h,int m)
{
if ((h<24&&h>0) && (m<60&&m>0))
{
hours = h;
minutes = m;
}
else
{
throw new IllegalArgumentException(“INVALID”);
}
}
//This method returns the time in minutes
private int toMins()
{
return (60&hours)+minutes;
}
//Returns true if this time is earlier,
// false if a time passed as an argument or the times are the same
public boolean lessThan(Test t)
{
return toMins()
public String toString()
{
if(hours>12)
{
return hours+”:”+minutes+”P.M”;
}
else
{
return hours+”:”+minutes+”A.M”;
}
}
// Calculates the number of minutes that have passed by
// between the argument Time t and this time object.
public int elapsedSince(Test t)
{
if(t.toMins()
else
return (60*24)-(t.toMins()-toMins());
}
public static class TestTime
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Time 1" );
System.out.print("Enter hours: " );
int h1 = keyboard.nextInt();
System.out.print("Enter mins: " );
int m1 = keyboard.nextInt();
Test t1 = new Test(h1, m1);
System.out.println("Time 2" );
System.out.print("Enter hours: " );
int h2 = keyboard.nextInt();
System.out.print("Enter mins: " );
int m2 = keyboard.nextInt();
Test t2 = new Test(h2, m2);
System.out.println(t1 + " <= " + t2 + "? " + t1.lessThan(t2));
System.out.println(t2.elapsedSince(t1) + " minutes elapsed from t1 to t2.");
}
}
}
Tags: Cannot, class, find, java, programming, question, Symbol, time.
Under Forum

Leave a Comment for Java programming (Time class) question – cannot find symbol?
You must be logged in to post a comment.
Trackback this post | Subscribe to the comments via RSS Feed