0% found this document useful (0 votes)
14 views1 page

Worksheet Program Answer

Program 1 defines a class 'Number' that assigns a value to an integer, checks if it is even or odd, and displays the result. Program 2 defines a class 'Diagonal' that calculates the diagonal of a 3D shape, the volume of a sphere, and checks if 27 is a multiple of 3. Both programs demonstrate object-oriented programming concepts in Java.

Uploaded by

darlinmary82
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)
14 views1 page

Worksheet Program Answer

Program 1 defines a class 'Number' that assigns a value to an integer, checks if it is even or odd, and displays the result. Program 2 defines a class 'Diagonal' that calculates the diagonal of a 3D shape, the volume of a sphere, and checks if 27 is a multiple of 3. Both programs demonstrate object-oriented programming concepts in Java.

Uploaded by

darlinmary82
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/ 1

Program 1:

public class Number


{
int n;
String x;
void assign()
{
n=5;
}
void calculate()
{
x = (n % 2 == 0) ? "Even" : "Odd";
}
void display()
{
System.out.println("The number is: " + x);
}
public static void main(String[] args)
{
Number num = new Numer();
num.assign();
num.calculate();
num.display();
}
}
Program 2:
public class Diagonal
{
int l, b, h, r;
double d, v;
String multipleCheck;
void assign()
{
l = 3; b = 4; h = 5; r = 6;
}
void calculate()
{
d = Math.sqrt(l * l + b * b + h * h);
v = Math.PI * Math.pow(r, 3);
multipleCheck = (27 % 3 == 0) ? "27 is a multiple of 3" : "27 is not a multiple of 3";
}
void display()
{
System.out.println("Diagonal (d): " + d);
System.out.println("Volume (v): " + v);
System.out.println (multipleCheck);
}
public static void main(String[] args)
{
Diagonal diag = new Diagonal();
diag.assign();
diag.calculate();
diag.display();
}
}

You might also like