Monday 23 March 2015

Calculating Rectangle Using User-defined Class in Java



The question is
  • Write a class named Rectangle to represent rectangle objects. The UML diagram for the class is shown below.  Then, write a program to test the class Rectangle. In this program, create three Rectangle objects. The first object has width 4 and height 40 while the second object has width 25 and height 20.5.  Assign any colors that you like. The third object has 1 for both width and height but the color is red. For this object, use the first constructor and then change the color to red using the appropriate method. Display the properties of these objects including their areas, perimeters and diagonal lengths.

dThe here is I declare Rectangle Class.



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Exercise2;

/**
 *
 * @author Achmad Zulkarnain
 */
public class Rectangle {

    double width;
    double height;
    String color;

    public Rectangle() { //Default Constructor
        width = 1;
        height = 1;
        color = "green";
    }

    public Rectangle(double w, double h, String s) { //constructor
        width = w;
        height = h;
        color = s;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double w) {
        width = w;
    }

    public double getheight() {
        return height;
    }

    public void setHeight(double h) {
        height = h;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String c) {
        color = c;
    }

    public double getArea() {
        return width*height;
    }

    public double getPerimeter() {
        return 2 * (width + height);
    }

    public double getDiagonal() {
        return Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
    }
}


here I create another class which contain main method. Main method combine everything and which method should run.


package Exercise2;


public class javamain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Rectangle obj1 = new Rectangle(4, 40, "Purple");
        Rectangle obj2 = new Rectangle(25, 20.5, "Blue");
        Rectangle obj3 = new Rectangle();
        
        
        //Object 1
        System.out.println("The first object has width: "+obj1.getWidth());
        System.out.println("The first object has height: "+obj1.getheight());
        System.out.println("The first object has color: "+obj1.getColor());
        System.out.println("The area of the first object is "+obj1.getArea());
        System.out.println("The parimeter of this  object is "+obj1.getPerimeter());
        System.out.println("The diagonal object is "+obj1.getDiagonal());
        
        System.out.println("#######################################");
        //Object 2
        System.out.println("The second object has width: "+obj2.getWidth());
        System.out.println("The second object has height: "+obj2.getheight());
        System.out.println("The second object has color: "+obj2.getColor());
        System.out.println("The area of the second object is "+obj2.getArea());
        System.out.println("The parimeter of this  object is "+obj2.getPerimeter());
        System.out.println("The diagonal object is "+obj2.getDiagonal());
        System.out.println("#######################################");
        
        System.out.println("The third object has width: "+obj3.getWidth());
        System.out.println("The third object has height: "+obj3.getheight());
        obj3.setColor("Red");
        System.out.println("The third object has color: "+obj3.getColor());
        System.out.println("The area of the third object is "+obj3.getArea());
        System.out.println("The parimeter of this  object is "+obj3.getPerimeter());
        System.out.println("The diagonal object is "+obj3.getDiagonal());
    }
    
}

Share:

Identify First Name and Surname (Bin and Binti) in Java

Maybe in Islam name, people has father's name after their name. For man, use "bin" and for woman use "binti". And how to identify name in Java using bin or binti??

boolean found = Arrays.asList(name.split(" ")).contains(keyword);

Code above is to identify whether in that name contain words in keyword. if the value is true then execute the program. If false, then execute something else.

here is the code in Java, I used array and use keyword to determine.

import java.util.Arrays;
import java.util.Scanner;

/**
 *
 * @author Achmad Zulkarnain
 */
public class Surname {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String keyword = "binti";

        System.out.print("Please enter your full name> ");
        String name = in.nextLine();

        boolean found = Arrays.asList(name.split(" ")).contains(keyword);
        if (found) {
            int Index = name.indexOf("binti");
            System.out.println("Hello your name is " + name.substring(0, (Index)));
            System.out.println("And your father's name is " + name.substring(Index + 6));
        } else {
            int Index = name.indexOf("bin");
            System.out.println("Hello your name is " + name.substring(0, (Index)));
            System.out.println("And your father's name is " + name.substring(Index + 4));
        }
    }
}



Share: