Friday, January 5, 2018

Working with Classes(2)

Challenge 1:  Write a program that defines and tests a class Square. In the class, there must be a method to find the area of the square and a method to print the information of the Square object. Choose appropriately the instance variable(s) for this real-world object. 

import java.util.Scanner;

class lab12_01 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println("Enter the side of the square: ");

        double side = in.nextDouble();





        Square a = new Square();

        a.findSquare(side);

        a.printSquare();
    }
}
class Square {

    double area;

    void findSquare(double side) {

        area = Math.pow(side, 2);

    }

    void printSquare() {
        System.out.println("This is the area of the square: " + area);
    }
}

Challenge 2:  Implement and test a class Employee. An employee has a first name (String), a last name (String) and a salary (double). A method raiseSalary and a method printEmployee will be implemented. The method raiseSalary will raise an employee salary by the amount passed to the method as an argument value. Sample usage of the class Employee:
Employee john = new Employee(); //initial salary = 30,000 for example john.raiseSalary(2000); //new value of salary will be 32,000 


import java.util.Scanner;

class lab12_02 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        Employee john = new Employee("John", "Wick", 30000);

        john.raisedSalary(2000);

        john.printEmployee();
    }
}

class Employee {

    public Employee(String firstName, String lastName, double salary) {
        fName = firstName;
        lName = lastName;
        sal = salary;
    }

    double area;

    void raisedSalary(double raiseSalary) {

        sal += raiseSalary;
    }

    void printEmployee() {
        System.out.println("Employee Records:\nFirst name: " + fName + " Last name:" + lName + " Salary:" + sal);

    }
    private String fName, lName;
    double sal;
}





Challenge 3: Write a program that defines and tests a class Rectangle. In the class, there must be methods to calculate the area, to calculate the perimeter, and to print the information of the rectangle. Choose appropriately the instance variable(s) for this real-world object. 

import java.util.Scanner;

class lab12_03 {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.println("Enter the length and width of the Rectangle: ");
        
        System.out.println("Length: ");
        double length = in.nextDouble();
        System.out.println("Width: ");
        double width = in.nextDouble();

        Rectangle a = new Rectangle();

        a.findSquare(length, width);

        a.findPerimeter(length, width);
        
        a.printInfo();
    }
}

class Rectangle {

    double area, perimeter;

    void findSquare(double l, double w) {

        area = w * l;

    }
    
    void findPerimeter(double l, double w){
    
    perimeter = 2 * l +  2 * w;
    
    }

    void printInfo() {
        System.out.println("The area of the Rectangle is: " + area);




        System.out.println("The perimeter of the Rectangle is: " + perimeter);
    }

}

No comments:

Post a Comment