Friday, January 5, 2018

Working with Classes

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 and add a default and a parameterized constructor. 





import java.util.Scanner;
class lab13_01 {    public static void main(String[] args) {class Square {    private double side, area;    public Square(double side) {    void findSquare(double side) {    void printSquare() {}
import java.util.Scanner;
class lab13_02 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
class Employee {
    public Employee(String firstName, String lastName, double salary ) {
        sal += raiseSalary;
    void printEmployee() {
    }
}
import java.util.Scanner;
class lab13_03 {
    public static void main(String[] args) {
        System.out.println("Rectangle Area & Perimeter: ");
        a.findPerimeter(7,8);
        a.printInfo();
}
 public Rectangle(double length, double width) {
    public void setLength(double length) {
    public double getWidth() {
    public void setWidth(double width) {
    private double area, perimeter;
    void findSquare(double l, double w) {
        this.length = l;
    void findPerimeter(double l, double w) {
    }




    void printInfo() {
}

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.

        Scanner in = new Scanner(System.in);
        //Square area from the constructor
        System.out.println("Calling the constructor");
        Square a = new Square(5);
      System.out.println("************************************");
        System.out.println("Calling the methods");
        System.out.println("Enter the side of the square: ");
        a.findSquare(6);
        a.printSquare();
    }
}
        this.side = side;
        findSquare(side);
        printSquare();
    }


        this.side = side;
        System.out.println("This is the area of the square: " + area);
        System.out.println("Calling the constructor");
    }
        System.out.println("***Employee Records:***\nFirst name: " + fName + "\nLast name: " + lName + "\nSalary: " + sal);
    private String fName, lName;

        double area = Math.pow(side, 2);
        this.area = area;
    }

    }


Add a default and a parameterized constructor to the class Employee

        Employee john = new Employee("John", "Wick", 30000);
        john.raisedSalary(2000);
        john.printEmployee();
    }
}
        fName = firstName;
        lName = lastName;
        sal = salary;  
    }
  
    void raisedSalary(double raiseSalary) {
    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 and add a default and a parameterized constructor. 


        System.out.println("Calling the constructor");
class Rectangle {
        this.length = length;
        this.length = length;
        return width;
        this.width = width;


        Rectangle a = new Rectangle(5,7);
        
System.out.println("****************************************");
        System.out.println("Calling the methods");
        a.setLength(5);
        a.setWidth(5);
        a.findSquare(7,8);
    }

private double length, width;

        this.width = width;
        findSquare(length, width);
        findPerimeter(length, width);
        printInfo();
 }
    public double getLength() {
        return length;
    }

    }

    }

    }
        this.width = w;
        double area = this.length * this.width;
        this.area = area;
    }
         this.length = l;




        this.width = w;
        double perimeter = 2 * this.length + 2 * this.width;
        this.perimeter = perimeter;
        System.out.println("The area of the Rectangle is: " + this.area);
        System.out.println("The perimeter of the Rectangle is: " + this.perimeter);
    }

No comments:

Post a Comment