0% found this document useful (0 votes)
40 views11 pages

Assignment 1 PDF

Uploaded by

Hafiz Hakim
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)
40 views11 pages

Assignment 1 PDF

Uploaded by

Hafiz Hakim
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/ 11

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 House {


String ownerName;
boolean isMalaysian;

public static void main(String[] args) {


// create an instance object of the House class
House myHouse = new House();

// initialize the ownerName and isMalaysian variables


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

// display the values of the variables


System.out.println("Owner Name: " + myHouse.ownerName);
System.out.println("Is Malaysian: " + myHouse.isMalaysian);
}
}
OUTPUT

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:

public class MyClass {


// findSum method to calculate the sum of three integers
public int findSum(int a, int b, int c) {
return a + b + c;
}

// findSum method to print the sum of an array of floating-point elements


public void findSum(float[] arr) {
float sum = 0;
for (float num : arr) {
sum += num;
}
System.out.println("Sum of the array elements: " + sum);
}
}

INPUT

OUTPUT
ANSWER:

FULL CODING

import java.util.Scanner;

public class ThemePark {


private String name;
private String idNo;
private String category;
private boolean isChild;
private boolean member;

// 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;
}

public void setIdNo(String idNo) {


this.idNo = idNo;
}
public void setCategory(String category) {
this.category = category;
}

public void setChild(boolean isChild) {


this.isChild = isChild;
}

public void setMember(boolean member) {


this.member = member;
}

// Getter methods
public String getName() {
return name;
}

public String getIdNo() {


return idNo;
}

public String getCategory() {


return category;
}

public boolean isChild() {


return isChild;
}

public boolean isMember() {


return member;
}

// toString method
public String toString() {
return "Name: " + name + "\nID: " + idNo + "\nCategory: " + category +
"\nChild: " + isChild + "\nMember: " + member;
}

// Method to calculate charges


public double calCharges() {
double charges = 0.0;
if (category.equalsIgnoreCase("wild life")) {
charges = isChild ? 50.0 : 100.0;
} else if (category.equalsIgnoreCase("water park")) {
charges = isChild ? 30.0 : 60.0;
} else if (category.equalsIgnoreCase("night safari")) {
charges = isChild ? 40.0 : 80.0;
}

if (member) {
charges *= 0.9; // 10% discount for members
}

return charges;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter name: ");


String name = scanner.nextLine();

System.out.print("Enter ID: ");


String idNo = scanner.nextLine();

System.out.print("Enter category (wild life, water park, night safari): ");


String category = scanner.nextLine();

System.out.print("Is the person a child (true/false): ");


boolean isChild = scanner.nextBoolean();

System.out.print("Is the person a member (true/false): ");


boolean isMember = scanner.nextBoolean();

ThemePark themePark = new ThemePark(name, idNo, category, isChild, isMember);

System.out.println("\nTheme Park Information:");


System.out.println(themePark.toString());

double charges = themePark.calCharges();


System.out.println("Charges: $" + charges);

scanner.close();
}
}
INPUT

OUTPUT

You might also like