944,196 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4673
  • Java RSS
Jun 17th, 2005
0

Homework help

Expand Post »
Hi all,

I'm new here, and am currently taking an introductory programming course taught with Java 5. I really hate to come here, and bother people with my homework, but I really don't have anywhere else to turn at the moment. I have a couple of quick questions about some assignments. Any help you all could give me would be very much apreciated.

My first question concerns an assignment I have already completed, but had alot of problems with scanner commands on. We were given a shell program to copy, and modify, however there was a problem with a section of the program that I coppied not working correctly.

Here is the problem section:
Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2. import java.text.NumberFormat;
  3.  
  4. public class Salary
  5. {
  6. public static void main (String[] args)
  7. {
  8. double currentSalary; // employee's current salary
  9. double raise; // amount of the raise
  10. double newSalary; // new salary for the employee
  11. String rating; // performance rating
  12.  
  13. Scanner scan = new Scanner(System.in);
  14.  
  15. System.out.print ("Enter the current salary: ");
  16. currentSalary = scan.nextDouble();
  17. System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
  18. rating = scan.nextLine();

For some reason when the program ran the "rating = scan.nextLine();" was ignored. When the previous scan command "currentSalary = scan.nextDouble();" was removed from the program though the scan.nextLine(); command would work. I wound up using scan.next instead of scan.nextLine, and the program ran as intended.

I'm just curious why this happened.

My second question concerns a CodeLab(a programming Q & A website) question that I have been stuck on for quite some time. Here is the question:

Quote ...
A class named Clock has two instance variables: hours (type int ) and isTicking (type boolean) . Write a constructor that takes a reference to an existing Clock object as a parameter and copies that object's instance variables to the object being created.

Your task: Write a Clock constructor that makes this possible. Just write this constructor -- don't write the whole class or any code outside of the class!

For example, if cl is a reference to a Clock object, whose hours and isTicking values are 10 and false respectively, the expression new Clock(cl) results in a new Clock object, whose instance variables' values are also 10 and false.
I know how to write a constructor, my problem is using the previous instance as a parameter. I've played with this problem for hours, but haven't figured out a working constructor yet. If anyone could give me some kind of hint on what kind parameter type I should use for this it would be a big help.

Thanks in advance.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tenchi is offline Offline
4 posts
since Jun 2005
Jun 17th, 2005
0

Re: Homework help

On the first exercise, you should have used .next() to read the string instead of .readLine(). I'm not sure why that didn't work unless that method is only for the BufferedReader. Maybe someone can help clarify that for you.

Java Syntax (Toggle Plain Text)
  1. rating = scan.next();


On the second assignment, you should have something of the sort:

Java Syntax (Toggle Plain Text)
  1. variable 1;
  2. variable 2;
  3. public constructorName(Clock c)
  4. {
  5. this.variable1 = c.getVariable1();
  6. this.variable2 = c.getVariable2();
  7. }
  8. }

That is of course only the constructor. That should help you out a little. For me to help you out more, you'll have to show some work so I don't get yelled at for doing someones homework. One more thing, when I'm assigning those objects variables to the current objects, I'm guessing you have methods in the other class that returns the variables value.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 17th, 2005
0

Re: Homework help

Thank you very much. Turns out I was using the right parameter, and just forgetting the "c." infront of my getters. So it was easy to fix with your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tenchi is offline Offline
4 posts
since Jun 2005
Jun 18th, 2005
0

Re: Homework help

I was reading the api for Scanner and I could not find any readLine method. I think the reason it was allowed to compile, is somewhere down the line it inherits that method...But if it inherited it, it should be able to use it...?? I'm confused on that one. I know the readLine() method is suppose to be for the BufferedReader, but it didn't give us a syntax error.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 18th, 2005
0

Re: Homework help

You missunderstood my question conserning the scanner methods a bit. The problem command was nextLine, not readLine. I'll try and explain a little better.

As part of a larger program I need to scan a double and a string token, and for some reason the nextLine command would not work when proceeded by a nextDouble, however it would work if the proceeding nextDouble was removed from the program. So here are three short programs I used to test the issue, and the results...

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class STest
  5. {
  6. public static void main (String[] args)
  7. {
  8. double currentSalary; // employee's current salary
  9. String rating; // performance rating
  10.  
  11. Scanner scan = new Scanner(System.in);
  12.  
  13. System.out.print ("Enter the current salary: ");
  14. currentSalary = scan.nextDouble();
  15. System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
  16. rating = scan.nextLine();
  17. System.out.println(""+currentSalary);
  18. System.out.println(""+rating);
  19.  
  20. }
  21. }

This would scan the double, then print the double, and then end. Skipping the nextLine command in the process.

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class STest
  6. {
  7. public static void main (String[] args)
  8. {
  9.  
  10. String rating; // performance rating
  11.  
  12. Scanner scan = new Scanner(System.in);
  13.  
  14.  
  15. System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
  16. rating = scan.next();
  17. System.out.println(""+currentSalary);
  18.  
  19.  
  20. }
  21. }

Now without the proceeding nextDouble. It will scan the string, and print it.

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class STest
  6. {
  7. public static void main (String[] args)
  8. {
  9. double currentSalary; // employee's current salary
  10. String rating; // performance rating
  11.  
  12. Scanner scan = new Scanner(System.in);
  13.  
  14. System.out.print ("Enter the current salary: ");
  15. currentSalary = scan.nextDouble();
  16. System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
  17. rating = scan.next();
  18. System.out.println(""+currentSalary);
  19. System.out.println(""+rating);
  20.  
  21. }
  22. }

Finally, if I use next instead of nextLine. It scans both the double, and the string token, and prints both.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tenchi is offline Offline
4 posts
since Jun 2005
Jun 19th, 2005
0

Re: Homework help

Opps the second program should use the nextLine method not the next method so...

Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class STest
  6. {
  7. public static void main (String[] args)
  8. {
  9.  
  10. String rating; // performance rating
  11.  
  12. Scanner scan = new Scanner(System.in);
  13.  
  14.  
  15. System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
  16. rating = scan.nextLine();
  17. System.out.println(""+currentSalary);
  18.  
  19.  
  20. }
  21. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tenchi is offline Offline
4 posts
since Jun 2005
Oct 22nd, 2009
0
Re: Homework help
hey I am new in java and i want a program that makes the user enter his e_mail address like (user1@liu.edu.uk) and the program output it's:
country code: uk
master domain: edu
sub domain: liu
user name: user1
please can anny one made it??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
haidarsss is offline Offline
1 posts
since Oct 2009
Oct 22nd, 2009
0
Re: Homework help
Tenchi, your problem with the nextLine() not working is the same problem I recently explained to another user on Daniweb. I'll paste my response to him here since the problem you have is exactly identical.


"The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline."
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 23rd, 2009
0
Re: Homework help
Click to Expand / Collapse  Quote originally posted by haidarsss ...
hey I am new in java and i want a program that makes the user enter his e_mail address like (user1@liu.edu.uk) and the program output it's:
country code: uk
master domain: edu
sub domain: liu
user name: user1
please can anny one made it??
No, this forum does not allow members doing other's homework .
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Loading text file into JTable
Next Thread in Java Forum Timeline: capturing images using java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC