0% found this document useful (0 votes)
11 views4 pages

Question Lab W3

Uploaded by

muhd nurfakhri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Question Lab W3

Uploaded by

muhd nurfakhri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

QUESTION 1

Uml class diagram

Rectangle

-width : double
-height : double

+Rectangle()
+Rectangle(double,double)
+setWidth(double)
+setHeight(double)
+getArea():double
+getPerimeter(): double

coding

public class Rectangle {

private double width=1;

private double length=1;

private double area;

private double perimeter;

public Rectangle(){}

public Rectangle(double widthN, double lengthN){

width = widthN;

length = lengthN;

public void setWidth(double widthN) {

width = widthN;

public void setLength(double lengthN){

length = lengthN;

}
public double getWidth(){

return width;

public double getLength(){

return length;

public double getPerimeter(){

perimeter = (length * 2) + (width * 2);

return perimeter;

public double getArea(){

area = width * length;

return area;

public String toString(){

return "perimeter is " + getPerimeter() + "area is " + getArea();

---------------------------------------------------------------------------
-------------------------------------------

public class testRectangle {

public static void main(String args[]){

Rectangle obj1 = new Rectangle(4,40);

Rectangle obj2 = new Rectangle(3.5, 35.9);

System.out.println(obj1);

System.out.println(obj2));

}
QUESTION 2

coding

public class Length {

private double cm;

public Length() {

this.cm = 0.0;

public Length(double cm) {

this.cm = cm;

public double getCm() {

return cm;

public double getInch() {

return cm * 0.393701;

public double getMeter() {

return cm / 100;

public double getFeet() {

return cm * 0.0328084;

public String toString() {

return "Length(cm): " + cm + ", Inch: " + getInch() + ", Meter: " +
getMeter() + ", Feet: " + getFeet();

public static void main(String[] args) {


Length length1 = new Length(100);

Length length2 = new Length(50.8);

System.out.println("Length 1: " + length1);

System.out.println("Length 2: " + length2);

You might also like