Results 1 to 8 of 8
  1. #1
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Help solving a code!

    Hi,

    I have actually got to solve a problem,

    And here's my question;


    Write a Name class that implements Comparable and has two instance variables forename and surname both of type String. The constructor should interact with the user to set up values for the forename and surname instance variables (use the Input.getString method). Write equals, compareTo and toString methods for the Name class. Two names are considered equal if both the forename and the surname match. When comparing two names, if the surnames match then compare the forenames. The toString method should return a string surname, forename.

    Write a NameTest class that repeatedly creates two Name objects, outputs their values (using println), outputs the result of the call to equals (true or false) and then outputs whether or not the names are equal etc. based on calls to compareTo. Use the Repeat.repeat method to control the loop within NameTest.

    The comparisons should not be case-sensitive (so joe should be considered equal to Joe).

    The String class has the following useful methods
    boolean equalsIgnoreCase (String anotherString)
    int compareToIgnoreCase (String anotherString)

    Test your program with the following names
    joe jones JOE JONES true equals
    joe james james joe false less than
    james joe joe james false greater than
    joe jones joe james false greater then
    joe james joe jones false less than
    jim james joe james false less than
    joe james jim james false greater than


    My answer:

    I have actually got various sample codes; and I'm not sure how do I combine them in the right order for my answer?!

    Java Code:
    public class EmployeeTest{
        public static void main(String[] args){
            EmployeeList employees=new EmployeeList();
            int option;
            do{
                System.out.println("0: quit, 1: add, 2: display, 3: display total salary, 4: display from town");
                option=Input.getInteger("input option: ");
                switch (option){
                    case 0: break;
                    case 1: employees.add();
                            break;
                    case 2: employees.display();
                            break;
                    case 3: employees.displayTotalSalary();
                            break;
                    case 4: employees.displayFromTown();
                            break;
                }
            } while (option !=0);
        }
    }
    
    
    public class EmployeeList{
        private Employee[] employees;
        private int count;
        public EmployeeList(){
            int size=Input.getInteger("input list size: ");
            this.employees=new Employee[size];
        }
        public void add(){
        	// check space available
    	    if (this.count<this.employees.length){
    	    	Employee employee=new Employee();
    			boolean identifierMatches=false;
    			int index=0;
    			// check identifier is unique
    		    while (!identifierMatches && index<this.count)
    		  		if (this.employees[index++].equals(employee))
    					identifierMatches=true;
    		    if (identifierMatches)
    				System.out.println("identifier already in use");
    			else
    				this.employees[this.count++]=employee;
    		}
            else
                System.out.println("list full");
        }
        public void display(){
            int index=0;
            while(index<this.count)
                System.out.println(this.employees[index++]);
        }
        public void displayTotalSalary(){
            int index=0;
            int totalSalary=0;
            while(index<this.count)
                totalSalary+=this.employees[index++].getSalary();
            System.out.println("total salary: "+totalSalary);
        }
        public void displayFromTown(){
            int index=0;
            String town=Input.getString("input town: ");
            System.out.println("employees from: "+town);
            while(index<this.count) {
                if (this.employees[index].getTown().equalsIgnoreCase(town))
                    System.out.println(this.employees[index]);
                index++;
            }
        }
    }
    public class Employee{
        private int identifier;
        private String name;
        private String town;
        private int salary;
    
        public Employee(){
            this.identifier=Input.getInteger("input identifier: ");
            this.name=Input.getString("input name: ");
            this.town=Input.getString("input town: ");
    		this.salary=Input.getInteger("input salary: ");
        }
        public String getTown(){
            return this.town;
        }
        public int getSalary(){
            return this.salary;
        }
    	public String toString(){
    	 	return "id: "+this.identifier+"  name: "+this.name+"  town: "+this.town+"  salary: "+this.salary;
     	}
    	public boolean equals(Object otherObject){
    	 	return this.identifier==((Employee)otherObject).identifier;
    	}
    }
    Last edited by Norm; 02-11-2012 at 02:34 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Help solving a code!

    I'm not sure how do I combine them
    Can you explain what your problem is?
    Show the program's output, explain what is wrong with it and show what you want the output to be.

  3. #3
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: Help solving a code!

    Quote Originally Posted by Norm View Post
    Can you explain what your problem is?
    Show the program's output, explain what is wrong with it and show what you want the output to be.
    I want the output to be the following, when I Test my program with the following names
    joe jones JOE JONES true equals
    joe james james joe false less than
    james joe joe james false greater than
    joe jones joe james false greater then
    joe james joe jones false less than
    jim james joe james false less than
    joe james jim james false greater than


    But I have got bits and pieces of codes(the one I pasted in my previous post) which I'm not sure how to combine them to get the output. When I run the above codes, the program just says fatal error and doesn't give me any output, I dont know where am I going wrong or even if I am on the right track :/

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Help solving a code!

    the program just says fatal error
    Please copy and paste here the full text of the error message. That is needed to find where the problem is.

  5. #5
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: Help solving a code!

    Help solving a code!-error.jpg

    This is what I see

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Help solving a code!

    Look at all of the output in all of the frames in the IDE's window. One of them has a very important error message in it.
    Hint: The text of the message is in red

  7. #7
    Zahara is offline Member
    Join Date
    Feb 2012
    Location
    Scotland, United Kingdom
    Posts
    14
    Rep Power
    0

    Default Re: Help solving a code!

    Norm, Thanks for spotting that out. I actually took that off and it still shows the same error

    Help solving a code!-error.jpg

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Help solving a code!

    I actually took that off
    What did you change? The same message is still there.
    Did you read the text that was in red?
    Do you understand what it means?

    You must make some changes to your program to get rid of that error.

    Go to this link and read about the error you are getting:
    http://docs.oracle.com/javase/tutori...ems/index.html
    Last edited by Norm; 02-11-2012 at 03:20 PM.

Similar Threads

  1. Solving Clear Button
    By Ryan10 in forum New To Java
    Replies: 73
    Last Post: 04-13-2011, 05:04 AM
  2. I need help on solving an exercise from school
    By dekelm12 in forum New To Java
    Replies: 6
    Last Post: 12-31-2010, 12:23 PM
  3. Help Needed in Solving the Following Isuue.
    By raju.i in forum Advanced Java
    Replies: 3
    Last Post: 05-14-2010, 05:01 PM
  4. Solving a Maze
    By bdario1 in forum New To Java
    Replies: 4
    Last Post: 04-14-2010, 12:02 AM
  5. I need help solving this problem.
    By Felicia in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 08:40 PM

Posting Permissions

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