Assignment 1 PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

QUESTION 1

Write a program to calculate the area of a rectangle.

CODE:

import java.util.Scanner; // Import the Scanner class to read input from the user

public class RectangleArea {


public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a new Scanner object to read input
from the user

System.out.print("Enter the length of the rectangle: ");


double length = input.nextDouble(); // Read the length from the user and store it in the
'length' variable

System.out.print("Enter the width of the rectangle: ");


double width = input.nextDouble(); // Read the width from the user and store it in the
'width' variable

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

Write a program to calculate volume of a sphere.

CODING:

import java.util.Scanner;

public class SphereVolume {


public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a new Scanner object to read input
from the user

System.out.print("Enter the radius of the sphere: ");


double radius = input.nextDouble(); Read the radius from the user and store it in the
‘radius' variable

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

Write a program to display message pass or fail based on score.

CODING:

import java.util.Scanner;

public class PassFail {


public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Create a new Scanner object to read input
from the user

System.out.print("Enter your score: ");


double score = input.nextDouble(); // Read the score from the user and store it in the
'score' variable

if (score >= 60) { // Check if the score is greater than or equal to 60


System.out.println("Congratulations, you passed!"); // If so, print a passing message to
the console
} else {
System.out.println("Sorry, you failed. Please try again."); // Otherwise, print a failing
message to the console
}
}
}

OUTPUT;
ANSWER:

a)

public class Main {


public static void main(String[] args) {
House myHouse = new House(); // Create an instance object of the House class (i)

// Initialize the properties of the object (ii)


myHouse.ownerName = "John";
myHouse.isMalaysian = true;

// Display the values of the object's properties (iii)


System.out.println("Owner name: " + myHouse.ownerName);
System.out.println("Is Malaysian: " + myHouse.isMalaysian);
}
}
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:

// First overloaded method


public int findSum(int num1, int num2, int num3) {
int sum = num1 + num2 + num3;
return sum;
}

// Second overloaded method


public void findSum(double[] nums) {
double sum = 0;
for (double num : nums) {
sum += num;
}
System.out.println("The sum of the array elements is: " + sum);
}
a)i)

public String toString() {


return "Name: " + name + "\nID: " + idNo + "\nCategory: " + category + "\nIs Child: " + isChild
+ "\nIs Member: " + member;
}
ii)

public double calCharges() {


double charge = 0.0;
if (category.equalsIgnoreCase("wild life")) {
charge = 50.0;
} else if (category.equalsIgnoreCase("water park")) {
charge = 30.0;
} else if (category.equalsIgnoreCase("night safari")) {
charge = 40.0;
}

if (isChild) {
charge /= 2;
}

if (member) {
charge *= 0.8;
}

if (idNo.startsWith("X")) {
charge *= 1.5;
}

return charge;
}

ANSWER:

// Declare array of ThemePark objects


System.out.print("Enter number of visitors: ");
int numVisitors = input.nextInt();
ThemePark[] visitors = new ThemePark[numVisitors];
// Create ThemePark objects and calculate charges
double totalWildlifeCharges = 0.0;
double totalWaterParkCharges = 0.0;
double totalNightSafariCharges = 0.0;

for (int i = 0; i < numVisitors; i++) {


System.out.print("Enter name of visitor " + (i+1) + ": ");
input.nextLine(); // consume newline character
String name = input.nextLine();

System.out.print("Enter ID of visitor " + (i+1) + ": ");


String id = input.nextLine();

System.out.print("Enter category of visitor " + (i+1) + " (wildlife, water park, or night safari):
");
String category = input.nextLine();

System.out.print("Is visitor " + (i+1) + " a child (true/false)? ");


boolean isChild = input.nextBoolean();

System.out.print("Is visitor " + (i+1) + " a member (true/false)? ");


boolean isMember = input.nextBoolean();

visitors[i] = new ThemePark(name, id, category, isChild, isMember);


double charges = visitors[i].calCharges();

if (category.equalsIgnoreCase("wildlife")) {
totalWildlifeCharges += charges;
} else if (category.equalsIgnoreCase("water park")) {
totalWaterParkCharges += charges;
} else if (category.equalsIgnoreCase("night safari")) {
totalNightSafariCharges += charges;
}
}

// Display total charges for each category


System.out.println("Total charges for wildlife category: RM " + totalWildlifeCharges);
System.out.println("Total charges for water park category: RM " + totalWaterParkCharges);
System.out.println("Total charges for night safari category: RM " + totalNightSafariCharges);

// Display information for adult visitors


System.out.println("Information for adult visitors:");
for (int i = 0; i < numVisitors; i++) {
if (!visitors[i].getChild()) {
System.out.println(visitors[i].toString());
}
}
}

You might also like