Chapter 3 Homework Answers


Chapter 3 – Implementing Classes
Home Work Answers
R3.16

Consider the following implementation of a class Square:
public class Square
{
private int sideLength;
private int area;
public Square(int initialLength)
{
sideLength = initialLength;
area = sideLength * sideLength;
}
public int getArea() { return area; }
public void grow() { sideLength = 2 * sideLength; }
}

What error does this class have? How would you fix it?

public class Square {
    private int sideLength;
    public int area;
    public Square  (int initialLength) {
        sideLength = initialLength;
    }
    // to Correct it
    public int getArea ( ) {
        area = sideLength * sideLength;
        return area;
    } }


E3.9
 Add a method printReceipt to the CashRegister class. The method should print the prices of all purchased items and the total amount due. Hint: You will need to form a string of all prices. Use the concat method of the String class to add additional items to that string. To turn a price into a string, use the call String.valueOf(price).

public class CashRegister {
    double price;
    String purchased;
    double total;
    String list = "";

    public void getInfo (String purch, double p) {
        purchased = purch;
        price = p;
        total = total + price;
        list = list + "\n" + purchased.concat(String.valueOf(price));
    }
    public void printReceipt ( ) {
        System.out.println("Purchased Items:");
        System.out.println(list);
        System.out.println("The total amount: " + total);
    }
}


// to test CashRegister class

public class CashTest {
    public static void main(String[] args) {
        CashRegister me = new CashRegister( );
        me.getInfo("Chocalate ",10);
        me.getInfo("Chips ",5);
        me.getInfo("Pepsi ",15);            
        me.getInfo("chocalate ",8);
        me.getInfo("chocalate ",12);
        me.printReceipt( );
    }
}
                            ( Run Result )



E3.11
Implement a class Employee. An employee has a name (a string) and a salary (a double).
Provide a constructor with two arguments public Employee(String employeeName, double currentSalary) and methods
public String getName()
public double getSalary()
public void raiseSalary(double byPercent)
These methods return the name and salary, and raise the employee’s salary by a certain percentage. Sample
usage:
Employee harry = new Employee("Hacker, Harry", 50000);
harry.raiseSalary(10); // Harry gets a 10 percent raise

Supply an EmployeeTester class that tests all methods.


public class Employee {
    String name;
    double salary;

    public Employee (String employeeName, double currentSalary) {
        name = employeeName;
        salary = currentSalary;
    }
    public String getName ( ) {             
        return name; 
    }
    public double getSalary ( ) {
        return salary;
    }

    public void raiseSalary (double byPercent) {
        salary = salary + salary * byPercent / 100;
    }     
}        
                                                  
// to test Employee class 

public class EmployeeTester {
    public static void main (String[] args) {
        Employee eyad = new      Employee("Eyad ayman", 5000);
        System.out.println("Employee Name: "+eyad.getName());
        System.out.println("Employee Salary: "+eyad.getSalary());
        eyad.raiseSalary(10);
        System.out.println("Reise Salary: "+eyad.getSalary());

}
 
}
                            ( Run Result )


P3.4
A microwave control panel has four buttons: one for increasing the time by 30
seconds, one for switching between power levels 1 and 2, a reset button, and a start
button. Implement a class that simulates the microwave, with a method for each button.
The method for the start button should print a message “Cooking for ... seconds

at level ...”.

public class Microwave {
   int time = 0;
   int  switch_b = 1;
   public void start( ) {
       System.out.println("Cooking for " +time+ " seconds\n" + "at level "+switch_b);   }
    public void increas( )  {
        time = time + 30;         }
        public void switching( ) {
           if (switch_b == 1)
            switch_b = 2;
        else
            switch_b = 1;     }
        public void reset() {
            time= 0;     }
}
   
public class MicrowaveTest {
    public static void main(String[] args) {
        Microwave m = new Microwave();
        m.switching();
        m.increas();
        m.start();
        m.reset();   }
   }
                            ( Run Result )


End of Answers 

Comments

Post a Comment

Popular posts from this blog

Chapter 1 Homework Answers

Chapter 4&5 Homework Answers