Results 1 to 5 of 5
Thread: Need help with a HW assignment
- 02-16-2011, 11:55 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Need help with a HW assignment
Hi i'm new to programming and am stuck with a homework assignment. Here is the assignment.
I'm getting errors and i'm not too sure what they mean. Here is the code I wrote so far.Define a class Employee with the following properties. Every Employee has a name and a salary. The constructor creates an Employee with a name and an initial salary. The method salary_change(amount) will increase or decrease the salary by the specified amount. The method quit() will set the salary of the Employee to zero. The method get_info() will return the current salary of the Employee. Write a program that lets a user create Employee objects and adjust their salaries (by raises or pay cuts). After each user command, the program must print the current salary of the Employee.
Here are the errors im getting.Java Code:import java.util.*; public class Employee { String name; double salary; static Scanner console = new Scanner(System.in); //constructor Employee(String n, double sal) { name = n; salary = sal; } void salary_change(double amount) { char choice; System.out.println("Is your employee getting a raise[Y/N]?"); choice = console.nextchar(); //if its a raise then add it to the salary if(choice == 'Y' || choice == 'y') { System.out.println("How much is the raise?"); amount = console.nextDouble(); salary = salary + amount; } //if it's a decrease then subtract it from the salary else { System.out.println("How much is the decrease?"); amount = console.nextDouble(); salary = salary - amount; } } public static double get_info() { System.out.println("What is the employee's salary?"); emp.salary = console.nextDouble(); return emp.salary; } void quit() { salary = 0; } public static void main(String[] args) { Employee emp = new Employee(); char choice1, choice2; double num; do { //gets employee name System.out.println("What is the name of your employee?"); emp.name = console.next(); emp.salary = get_info(); //gets the ammount of the raise/paycut System.out.println("How much is the raise/paycut?"); num = console.nextDouble(); System.out.println("Is your employee getting a raise[Y/N]?"); choice1 = console.nextchar(); emp.salary_change(num); //prints new salary System.out.println("There new salary is " + emp.salary); //resets salary to 0 emp.quit(); System.out.println("Would you like to enter another employee[Y/N]?"); choice2 = console.nextchar(); //if yes loops back to the top } while(choice2 == 'Y' || choice2 == 'y'); } }
Employee.java:24: cannot find symbol
symbol : method nextchar()
location: class java.util.Scanner
choice = console.nextchar();
^
Employee.java:44: cannot find symbol
symbol : variable emp
location: class Employee
emp.salary = console.nextDouble();
^
Employee.java:46: cannot find symbol
symbol : variable emp
location: class Employee
return emp.salary;
^
Employee.java:57: cannot find symbol
symbol : constructor Employee()
location: class Employee
Employee emp = new Employee();
^
Employee.java:70: cannot find symbol
symbol : method nextchar()
location: class java.util.Scanner
choice1 = console.nextchar();
^
Employee.java:78: cannot find symbol
symbol : method nextchar()
location: class java.util.Scanner
choice2 = console.nextchar();
^
6 errors
What am I doing wrong? any help would be appriciated.Last edited by mackavelirip; 02-17-2011 at 12:09 AM.
- 02-17-2011, 12:10 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
All methods are done with camel case, so nextchar() is incorrect.
Also, you did not define a default employee constructor and then used one. If you do not create a constructor the compiler will supply a default constructor for you, however, the second you create a constructor, it will no longer provide a default one for you. Default means no-arg constructor.
The final thing, you don't need to refer to stuff as
in the classes methods, You can usually do this.salary, or simply do salary.Java Code:emp.salary
When you apply the methods they will take form of
so in the method someMethod, when you say salary, it means someEmp.salary.Java Code:someEmp.someMethod()
The goal of get_info is to display the result of a created employee so it should not be changing anything.
It's easier think of getters and setters, a getter reads info and allows you to use it, and setters allow you to change them.
Java Code:public class MyClass{ private int x; private String z; //default constructor MyClass(){ x = 0; z = ""; } //arg taking constructor MyClass(int x, String z){ this.x = x; this.z = z; } //getters public String getZ(){ return z; } public int getX(){ return x; } //setter public void setZ(String z){ this.z = z; } public void setX(int x){ this.x = x; } }
- 02-17-2011, 01:27 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Thanks for your help. I think I did everything you said but when I run it it doesn't let the user input the salary it just skips it. And also when I ask if its a raise yes or no i have to type either n or y twice before it proceeds.
Java Code:import java.util.*; public class Employee { String name; double salary; static Scanner console = new Scanner(System.in); Employee() { name = ""; salary = 0; } //constructor Employee(String n, double sal) { name = n; salary = sal; } void salary_change(double amount) { char choice; System.out.println("Is your employee getting a raise[Y/N]?"); choice = console.next().charAt(0); //if its a raise then add it to the salary if(choice == 'Y' || choice == 'y') { System.out.println("How much is the raise?"); amount = console.nextDouble(); salary = salary + amount; } //if it's a decrease then subtract it from the salary else { System.out.println("How much is the decrease?"); amount = console.nextDouble(); salary = salary - amount; } } public double get_info() { return salary; } void quit() { salary = 0; } public static void main(String[] args) { Employee emp = new Employee(); char choice1, choice2; double num; do { //gets employee name System.out.println("What is the name of your employee?"); emp.name = console.nextLine(); System.out.println("What is their salary?"); emp.get_info(); //gets the ammount of the raise/paycut System.out.println("How much is the raise/paycut?"); num = console.nextDouble();800 System.out.println("Is your employee getting a raise[Y/N]?"); choice1 = console.next().charAt(0); emp.salary_change(num); //prints new salary System.out.println("There new salary is " + emp.salary); //resets salary to 0 emp.quit(); System.out.println("Would you like to enter another employee[Y/N]?"); choice2 = console.next().charAt(0); //if yes loops back to the top } while(choice2 == 'Y' || choice2 == 'y'); } }
-
What happens if you put a console.nextLine(); after every line that has console.next() or console.nextDouble()? Try it and report back to us.
- 02-17-2011, 01:36 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Having a scanner object in the class seems very odd. It may be alright to use it, but Id suggest you not use a setup like that.
As far as the raise method, you can change it to something that takes a String and a double, or simply a double. The string can be up/down, y/n, or whatever else you want. Or you can allow negative input numbers to decrease salary and positive to increase them.
Try putting the scanner in main and taking the static scanner out of the class, then you can prompt for what you want.
Another interesting thing you can do is set up a menu
You can then use a user input and a switch statement to do the correct thing.Java Code:System.out.println("1. Create some object"); System.out.println("2. Change some object");
The code however looks pretty well done, Im not sure if you are getting error messages or what, but consider removing the static scanner from the class and creating one in main.
Similar Threads
-
assignment help
By esallender in forum New To JavaReplies: 4Last Post: 10-25-2010, 12:10 PM -
help with an assignment!
By Tek in forum New To JavaReplies: 6Last Post: 10-24-2010, 11:32 PM -
Need help with assignment! please
By runawaykinms in forum Java AppletsReplies: 2Last Post: 10-06-2010, 09:58 AM -
GUI First Assignment-DUE 8/1/08
By ljk8950 in forum AWT / SwingReplies: 2Last Post: 08-01-2008, 04:23 AM -
First GUI Assignment
By ljk8950 in forum New To JavaReplies: 1Last Post: 07-31-2008, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks