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:
import java.util.Scanner;
import java.text.NumberFormat;
public class Salary
{
public static void main (String[] args)
{
double currentSalary; // employee's current salary
double raise; // amount of the raise
double newSalary; // new salary for the employee
String rating; // performance rating
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the current salary: ");
currentSalary = scan.nextDouble();
System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
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:
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.