What's Wrong With My Code?
Please help me, I cannot find the error in the following code. Boolean always returs true in the following program.
Code :
class time
{
int hh, mm, ss;
public time()
{
hh = 0;
mm = 0;
ss = 0;
}
public time(int h, int m, int s)
{
hh = h;
mm = m;
ss = s;
}
public boolean validate()
{
boolean valid = false;
if((hh>=0 && hh<=23)||(mm>=0 && mm<=59)||(ss>=0 && ss<=59))
valid=true;
return valid;
}
public void display()
{
if(hh>12)
hh=hh-12;
System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
}
public static void main(String args[])
{
time ob = new time(20,55,30);
if (ob.validate()==true)
ob.display();
else
System.out.println("Invalid format");
}
}
Re: What's Wrong With My Code?
Classes need to begin with a Capital letter. So in this case, "time" should be "Time". Change all the mentions of "time" to "Time" and all will be fine. Also I noticed that your validate() method is not exactly accurate. So long as ONLY ONE of the three conditions is satisfied, it will work. You need ALL THREE to be satisfied to be a correct time:
For your validate() method, replace the "||" with "&&":
Code java:
public boolean validate()
{
if((hh>=0 && hh<=23) && (mm>=0 && mm<=59) && (ss>=0 && ss<=59))
return true;
else
return false;
}
Following on from that, in the main program, you don't have to say if(ob.validate() == true)...you could just say if(ob.validate()). :)
Re: What's Wrong With My Code?
Quote:
Classes need to begin with a Capital letter
Not true. The only requirement is that once you name a class, you must stick to that name. It is generally recommended that you start classes with a capital letter, though. The fixed validate logic is correct (since you want to check if all fields are valid, not if one or more fields are valid).
Re: What's Wrong With My Code?
Thank you very much for helping me. now i have only one problem on my program. If i enter time >23, it should display "Invalid Format". but its is showing the time subtracting 12.
Here is the final code
Code :
class Time
{
int hh, mm, ss;
public Time()
{
hh = 0;
mm = 0;
ss = 0;
}
public Time(int h, int m, int s)
{
hh = h;
mm = m;
ss = s;
}
public boolean validate()
{
if((hh>=0 && hh<=23) && (mm>=0 && mm<=59) && (ss>=0 && ss<=59))
return true;
else
return false;
}
public void display()
{
if(hh>12)
hh=hh-12;
Time ob = new Time(20,55,30);
if (ob.validate())
System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
else
System.out.println("Invalid format");
}
}
Quote:
Originally Posted by
TopdeK
Classes need to begin with a Capital letter. So in this case, "time" should be "Time". Change all the mentions of "time" to "Time" and all will be fine. Also I noticed that your validate() method is not exactly accurate. So long as ONLY ONE of the three conditions is satisfied, it will work. You need ALL THREE to be satisfied to be a correct time:
For your validate() method, replace the "||" with "&&":
Code java:
public boolean validate()
{
if((hh>=0 && hh<=23) && (mm>=0 && mm<=59) && (ss>=0 && ss<=59))
return true;
else
return false;
}
Following on from that, in the main program, you don't have to say if(ob.validate() == true)...you could just say if(ob.validate()). :)
Re: What's Wrong With My Code?
Code Java:
if(hh>12)
hh=hh-12;
Time ob = new Time(20,55,30);
if (ob.validate())
You're automatically subtracting 12 from hh no matter if the format is valid or not. Secondly, you're storing that result back into hh. you should instead either store that result to a temporary variable, move the subtraction to after you've checked that you have a valid time, or remove it all together and have your program display it out in military time (since display() doesn't print out if it's AM or PM anyways).
Lastly, creating ob is useless since it has no bearing on the current object. Calling validate on that object will always be true since it's always going to be a valid time. Instead, call validate on your current time object.
Re: What's Wrong With My Code?
Thank you for pointing out the problems. I would be more greatful if you make the necessary correction on my codes as I am very new in Java programs.
Quote:
Originally Posted by
helloworld922
Code Java:
if(hh>12)
hh=hh-12;
Time ob = new Time(20,55,30);
if (ob.validate())
You're automatically subtracting 12 from hh no matter if the format is valid or not. Secondly, you're storing that result back into hh. you should instead either store that result to a temporary variable, move the subtraction to after you've checked that you have a valid time, or remove it all together and have your program display it out in military time (since display() doesn't print out if it's AM or PM anyways).
Lastly, creating ob is useless since it has no bearing on the current object. Calling validate on that object will always be true since it's always going to be a valid time. Instead, call validate on your current time object.
Re: What's Wrong With My Code?
Quote:
Originally Posted by
helloworld922
Not true. The only requirement is that once you name a class, you must stick to that name. It is generally recommended that you start classes with a capital letter, though. The fixed validate logic is correct (since you want to check if all fields are valid, not if one or more fields are valid).
Really? My lecturer needs to get his facts right then. :P Thanks for the clarity
Re: What's Wrong With My Code?
Yep. The name can start with any letter (upper or lower case), an underscore, or some currency symbols (such as the british pound symbol or dollar symbol). Actually, any valid name for a variable/method is automatically a valid name for a class (and vise-versa). Same goes for interfaces, enums, etc. Of course, these names probably shouldn't be used because they can get extremely confusing.
Code Java:
class _
{
public static void main(String[] $)
{
System.out.println($[0]);
}
}
Quote:
I would be more greatful if you make the necessary correction on my codes as I am very new in Java programs.
I try not to provide the full solution because I believe you won't learn how to fix your code if I just dump the code. See if you can't take the description I gave and modify your code accordingly.
To call a member method of the same class, you can leave of all object quantifiers, or use the "this" keyword:
Code Java:
if(this.validate()) // using the this keyword.
{
// this time is valid. do stuff in here
}
Re: What's Wrong With My Code?
Re: What's Wrong With My Code?
Looked up the java language standard, and it turns out that any unicode character which isn't a number or pre-defined java symbol is considered a "JavaLetter", and thus can be used in the name. This is to support writing code in foreign languages, for example Russian, Greek, Korean, etc.
Re: What's Wrong With My Code?
i have made my code as follows... now the question is how can i display time as 12 hour format?
Code :
public void display()
{
// if(hh>12)
// hh=hh-12;
// Time ob = new Time();
// if (ob.validate())
if(this.validate()) // using the this keyword.
System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
else
System.out.println("Invalid format");
}
}
Quote:
Originally Posted by
helloworld922
Yep. The name can start with any letter (upper or lower case), an underscore, or some currency symbols (such as the british pound symbol or dollar symbol). Actually, any valid name for a variable/method is automatically a valid name for a class (and vise-versa). Same goes for interfaces, enums, etc. Of course, these names probably shouldn't be used because they can get extremely confusing.
Code Java:
class _
{
public static void main(String[] $)
{
System.out.println($[0]);
}
}
I try not to provide the full solution because I believe you won't learn how to fix your code if I just dump the code. See if you can't take the description I gave and modify your code accordingly.
To call a member method of the same class, you can leave of all object quantifiers, or use the "this" keyword:
Code Java:
if(this.validate()) // using the this keyword.
{
// this time is valid. do stuff in here
}
Re: What's Wrong With My Code?
You can display in a 12 hour format by creating a temporary hour variable, and a temporary string variable to hold "am" or "pm". This variable would hold the value of hh if hh is less than or equal to 12 and the string should contain "am". If hh is greater than 12, the temporary hour variable should hold hh-12 and the string should contain "pm". If hh is 0, the temporary hour variable should hold 12 and the string should contain "am".
Then, your output code would print out:
Code Java:
int tempHour = 0;
String period = null; // either "am" or "pm"
// TODO: initialize tempHour and period according to my descriptions
System.out.println("Your time is: "+ tempHour +":"+ mm +":"+ ss + period);
Re: What's Wrong With My Code?
Quote:
Originally Posted by
TopdeK
Really? My lecturer needs to get his facts right then. :P Thanks for the clarity
Someone who knows more than I can correct me, but classes do not need to begin with a capital letter in the sense that is a compile-time or run-time error if they don't. However, it is a coding convention that they do, because it makes all <ClassName> type readily distinguishable from instance variables.
Re: What's Wrong With My Code?
Yes, that's correct. That is indeed the recommended convention.
Re: What's Wrong With My Code?
Sorry! i cannot apply the tecnique in my program. please help me
Quote:
Originally Posted by
helloworld922
You can display in a 12 hour format by creating a temporary hour variable, and a temporary string variable to hold "am" or "pm". This variable would hold the value of hh if hh is less than or equal to 12 and the string should contain "am". If hh is greater than 12, the temporary hour variable should hold hh-12 and the string should contain "pm". If hh is 0, the temporary hour variable should hold 12 and the string should contain "am".
Then, your output code would print out:
Code Java:
int tempHour = 0;
String period = null; // either "am" or "pm"
// TODO: initialize tempHour and period according to my descriptions
System.out.println("Your time is: "+ tempHour +":"+ mm +":"+ ss + period);
Re: What's Wrong With My Code?
What part are you having trouble with?
You can perform multiple statements in one if statement by using brackets.
Code Java:
if(condition== true)
{ // execute all statements in here if condition is true, otherwise do none of them
System.out.println("doing statement 1");
System.out.println("doing statement 2");
}
Re: What's Wrong With My Code?
I am feeling shy now!
I changed my code as follows... but its incorrect. please help me
Code :
public void display()
{
if(this.validate()) // using the this keyword.
{
if(hh>12)
{
hh=hh-12;
}
System.out.println("Your time is: "+ hh+":"+ mm +":"+ ss);
}
else
System.out.println("Invalid format");
}
Quote:
Originally Posted by
helloworld922
What part are you having trouble with?
You can perform multiple statements in one if statement by using brackets.
Code Java:
if(condition== true)
{ // execute all statements in here if condition is true, otherwise do none of them
System.out.println("doing statement 1");
System.out.println("doing statement 2");
}
Re: What's Wrong With My Code?
Sorry, I'm not trying to intimidate you, but I do want you to think.
Ok, so first you have your if statement which checks to see if the time is valid:
Code Java:
if(this.validate())
{
// we have a valid time, do stuff in here
}
else
{
// invalid time
System.out.println("invalid time");
}
Now that you have a valid time, we want to create a local temporary variable we can modify without changing hh of the object. We also want a string to hold if we're in the morning or afternoon. For the sake of simplicity, let's assume it's am.
Code Java:
int temporaryHH = hh;
String period = "am"; // this is an assumption
After assuming it's am, we need to validate that assumption. How do we know if it's am? If temporaryHH is before noon (smaller than 12). Otherwise, it's pm. If it's pm, temporaryHH needs to be decreased by 12.
Code Java:
if(temporaryHH >= 12)
{
// pm
period = "pm";
temporaryHH = temporaryHH - 12;
}
The last thing to check is if temporaryHH is 0. What does it mean to be 0 am or 0 pm? That's the equivalent of 12 am or 12 pm. To see if you understand this, I'd like you to see if you can figure out how to accomplish this using the above code (remember to operate on temporaryHH, not hh).
Your final code should look something like this:
Code Java:
if(this.validate())
{
// we have a valid time
int temporaryHH = hh;
String period = "am"; // assume we're am
if(temporaryHH >= 12)
{
// actually in pm
period = "pm";
temporaryHH = temporaryHH - 12;
}
// TODO: what code should go here to fix if temporaryHH is 0?
// print out the valid time
System.out.println("Your time is: "+ temporaryHH +":"+ mm +":"+ ss + period);
}
else
{
// invalid time
System.out.println("invalid time");
}
If you use brackets like I did (indent in on each open bracket, indent out on each close bracket) you can easily tell if you're missing a bracket, as well as see what code belongs to which condition.
Re: What's Wrong With My Code?
Thank you Very Much. My problem is now solved.
Quote:
Originally Posted by
helloworld922
Sorry, I'm not trying to intimidate you, but I do want you to think.
Ok, so first you have your if statement which checks to see if the time is valid:
Code Java:
if(this.validate())
{
// we have a valid time, do stuff in here
}
else
{
// invalid time
System.out.println("invalid time");
}
Now that you have a valid time, we want to create a local temporary variable we can modify
without changing hh of the object. We also want a string to hold if we're in the morning or afternoon. For the sake of simplicity, let's assume it's am.
Code Java:
int temporaryHH = hh;
String period = "am"; // this is an assumption
After assuming it's am, we need to validate that assumption. How do we know if it's am? If temporaryHH is before noon (smaller than 12). Otherwise, it's pm. If it's pm, temporaryHH needs to be decreased by 12.
Code Java:
if(temporaryHH >= 12)
{
// pm
period = "pm";
temporaryHH = temporaryHH - 12;
}
The last thing to check is if temporaryHH is 0. What does it mean to be 0 am or 0 pm? That's the equivalent of 12 am or 12 pm. To see if you understand this, I'd like you to see if you can figure out how to accomplish this using the above code (remember to operate on temporaryHH, not hh).
Your final code should look something like this:
Code Java:
if(this.validate())
{
// we have a valid time
int temporaryHH = hh;
String period = "am"; // assume we're am
if(temporaryHH >= 12)
{
// actually in pm
period = "pm";
temporaryHH = temporaryHH - 12;
}
// TODO: what code should go here to fix if temporaryHH is 0?
// print out the valid time
System.out.println("Your time is: "+ temporaryHH +":"+ mm +":"+ ss + period);
}
else
{
// invalid time
System.out.println("invalid time");
}
If you use brackets like I did (indent in on each open bracket, indent out on each close bracket) you can easily tell if you're missing a bracket, as well as see what code belongs to which condition.