Professional Documents
Culture Documents
Assignment 1 PDF
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)
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:
if (isChild) {
charge /= 2;
}
if (member) {
charge *= 0.8;
}
if (idNo.startsWith("X")) {
charge *= 1.5;
}
return charge;
}
ANSWER:
System.out.print("Enter category of visitor " + (i+1) + " (wildlife, water park, or night safari):
");
String category = input.nextLine();
if (category.equalsIgnoreCase("wildlife")) {
totalWildlifeCharges += charges;
} else if (category.equalsIgnoreCase("water park")) {
totalWaterParkCharges += charges;
} else if (category.equalsIgnoreCase("night safari")) {
totalNightSafariCharges += charges;
}
}