How can I declare two variable?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 13
Reputation: M.Jama is an unknown quantity at this point 
Solved Threads: 0
M.Jama M.Jama is offline Offline
Newbie Poster

How can I declare two variable?

 
0
  #1
Oct 29th, 2009
I'm too new in java and I always appreciated when you share with me your knowledge. Here I'm trying following self-test exercise;

Give two declaration for two variables called feet and inches.Both variables are of type int and both are to be initialised to zero in declaration.
I try this way and it's compilable !! but not sure....did I miss something?

public class feetInches
{
public static void main(String [] args)
{
int feet = 0;
int inches = 0;
int totalFeet = inches * 12;

System.out.println("5 feet is " + 5* 12);


}

}
Thank you in advance
MJ
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: Sandar Khin is an unknown quantity at this point 
Solved Threads: 1
Sandar Khin's Avatar
Sandar Khin Sandar Khin is offline Offline
Newbie Poster
 
0
  #2
Oct 29th, 2009
Originally Posted by M.Jama View Post
I'm too new in java and I always appreciated when you share with me your knowledge. Here I'm trying following self-test exercise;

Give two declaration for two variables called feet and inches.Both variables are of type int and both are to be initialised to zero in declaration.
I try this way and it's compilable !! but not sure....did I miss something?

public class feetInches
{
public static void main(String [] args)
{
int feet = 0;
int inches = 0;
int totalFeet = inches * 12;

System.out.println("5 feet is " + 5* 12);


}

}
Thank you in advance
MJ

This program can compile and run.Don' t have program error.You should check other.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 13
Reputation: M.Jama is an unknown quantity at this point 
Solved Threads: 0
M.Jama M.Jama is offline Offline
Newbie Poster
 
0
  #3
Oct 29th, 2009
Originally Posted by Sandar Khin View Post
This program can compile and run.Don' t have program error.You should check other.

Yes, sandar it can compile. What I am asking is is there better way to do it?

Thanks

MJ
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #4
Oct 29th, 2009
  1. public class feetInches
  2. {
  3. public static void main(String [] args)
  4. {
  5. int feet = 5;
  6. int feetToInches = 12;
  7. int inches = feet * feetToInches ;
  8.  
  9. System.out.println(feet + " feet is " + inches);
  10. }
  11. }

If you want the user to enter input then search this forum for the Scanner class
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #5
Oct 29th, 2009
Sorry for the double posting but a better way would be:
  1. public class feetInches
  2. {
  3. public static final int FEET_TO_INCHES = 12;
  4. public static final double INCHES_TO_FEET = 1.0/12.0;
  5.  
  6. public static void main(String [] args)
  7. {
  8. int feet = 5;
  9. int inches = feet * FEET_TO_INCHES ;
  10.  
  11. System.out.println(feet + " feet is " + inches);
  12. }
  13. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 13
Reputation: M.Jama is an unknown quantity at this point 
Solved Threads: 0
M.Jama M.Jama is offline Offline
Newbie Poster
 
0
  #6
Oct 29th, 2009
Originally Posted by javaAddict View Post
  1. public class feetInches
  2. {
  3. public static void main(String [] args)
  4. {
  5. int feet = 5;
  6. int feetToInches = 12;
  7. int inches = feet * feetToInches ;
  8.  
  9. System.out.println(feet + " feet is " + inches);
  10. }
  11. }

If you want the user to enter input then search this forum for the Scanner class
Thanks JavaAddict
That's exactly what I needed.
Cheerz MJ
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 13
Reputation: M.Jama is an unknown quantity at this point 
Solved Threads: 0
M.Jama M.Jama is offline Offline
Newbie Poster
 
0
  #7
Oct 29th, 2009
would you mind also to help me this:

Write Java assignment statement that will set the value of the variable distance to value of the time multiplied by 80. All the variable are of type int.



public class timetoDistance

{

public static void main(String [] args)

{

int time;
Int distance;


}

}

???
Thank you in advance
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #8
Oct 29th, 2009
Originally Posted by M.Jama View Post
the time multiplied by 80
It's not that difficult. Multiply time by 80 and put it in the distance
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 13
Reputation: M.Jama is an unknown quantity at this point 
Solved Threads: 0
M.Jama M.Jama is offline Offline
Newbie Poster
 
0
  #9
Oct 29th, 2009
Originally Posted by javaAddict View Post
It's not that difficult. Multiply time by 80 and put it in the distance
JavaAddict,
sorry, to disturb you again. How can I build without having rate? e.g.

rt=d (rate multiplied by time = distance)

Thank you in advance

MJ
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #10
Oct 29th, 2009
Originally Posted by M.Jama View Post
JavaAddict,
sorry, to disturb you again. How can I build without having rate? e.g.

rt=d (rate multiplied by time = distance)

Thank you in advance

MJ
Your question doesn't make sense.
  1. int distance = 80 * time;
  2. // or
  3. int rate = 80;
  4. int time = ...;
  5. int distance = rate * time;
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 596 | Replies: 19
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC