Professional Documents
Culture Documents
Assignment 1 PDF
Assignment 1 PDF
CODE:
import java.util.Scanner; // Import the Scanner class to read input from the user
double area = length * width; // Calculate the area by multiplying the length and width,
and store it in the 'area' variable
System.out.println("The area of the rectangle is: " + area); // Print the area to the console
}
}
OUTPUT:
QUESTION 2
CODING:
import java.util.Scanner;
double volume = (4.0 / 3.0) * Math.PI * Math.pow(radius, 3); //Calculate the area by
multiplying the 4/3, PI and radius power of three, and store it in the 'volume' variable
System.out.println("The volume of the sphere is: " + volume); //Print the volume to the
console
}
}
OUTPUT:
QUESTION 3
CODING:
import java.util.Scanner;
OUTPUT;
ANSWER:
a)
ANSWER:
Class Variables:
● carNum (which is declared with the static keyword)
● price (which is declared with the static keyword)
Instance Variables:
● carNumber (which is not declared with the static keyword)
● carPrice (which is not declared with the static keyword)
ANSWER:
INPUT
OUTPUT
ANSWER:
FULL CODING
import java.util.Scanner;
// Constructors
public ThemePark() {
}
public ThemePark(String n, String id, String cat, boolean ch, boolean mem) {
name = n;
idNo = id;
category = cat;
isChild = ch;
member = mem;
}
// Setter methods
public void setName(String name) {
this.name = name;
}
// Getter methods
public String getName() {
return name;
}
// toString method
public String toString() {
return "Name: " + name + "\nID: " + idNo + "\nCategory: " + category +
"\nChild: " + isChild + "\nMember: " + member;
}
if (member) {
charges *= 0.9; // 10% discount for members
}
return charges;
}
scanner.close();
}
}
INPUT
OUTPUT