Results 1 to 5 of 5
  1. #1
    mackavelirip is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Need help with a HW assignment

    Hi i'm new to programming and am stuck with a homework assignment. Here is the assignment.

    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.
    I'm getting errors and i'm not too sure what they mean. Here is the code I wrote so far.

    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');
    
    		
    	}
    }
    Here are the errors im getting.

    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 01:09 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    emp.salary
    in the classes methods, You can usually do this.salary, or simply do salary.
    When you apply the methods they will take form of
    Java Code:
    someEmp.someMethod()
    so in the method someMethod, when you say salary, it means someEmp.salary.

    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;
      }
    }

  3. #3
    mackavelirip is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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');
    		
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    System.out.println("1. Create some object");
    System.out.println("2. Change some object");
    You can then use a user input and a switch statement to do the correct thing.


    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

  1. assignment help
    By esallender in forum New To Java
    Replies: 4
    Last Post: 10-25-2010, 01:10 PM
  2. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 12:32 AM
  3. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 10:58 AM
  4. GUI First Assignment-DUE 8/1/08
    By ljk8950 in forum AWT / Swing
    Replies: 2
    Last Post: 08-01-2008, 05:23 AM
  5. First GUI Assignment
    By ljk8950 in forum New To Java
    Replies: 1
    Last Post: 07-31-2008, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •