0% found this document useful (0 votes)
16 views

Lab 3

The document contains code for a Java program that defines a Number class with methods to check if a given number is zero, positive, negative, odd, even, prime or an Armstrong number. It then shows how to use the Number class in a main method by taking user input and printing the results of calling the methods.
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)
16 views

Lab 3

The document contains code for a Java program that defines a Number class with methods to check if a given number is zero, positive, negative, odd, even, prime or an Armstrong number. It then shows how to use the Number class in a main method by taking user input and printing the results of calling the methods.
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/ 6

Name :- Mohit Kumar

RollNo:-2200290120104
Sec :- B

Program :-7
Code:-
package mohit;

import java.util.Scanner;

class Number {
private int number;

Number(int num) {
this.number = num;
}

public boolean isZero() {


return number == 0;
}

public boolean isPositive() {


return number > 0;
}

public boolean isNegative() {


return number < 0;
}

public boolean isOdd() {


return number % 2 != 0;
}
public boolean isEven() {
return number % 2 == 0;
}

public boolean isPrime() {


if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}

public boolean isAmstrong() {


int n = number;
int temp = n;
int sum = 0;
int digits = (int) Math.log10(n) + 1;

while (temp > 0) {


int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}

return sum == n;
}
}

public class Program77 {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();

Number number = new Number(num);

System.out.println("isZero = " + number.isZero());


System.out.println("isPositive = " + number.isPositive());
System.out.println("isNegative = " + number.isNegative());
System.out.println("isOdd = " + number.isOdd());
System.out.println("isEven = " + number.isEven());
System.out.println("isPrime = " + number.isPrime());
System.out.println("isAmstrong = " + number.isAmstrong());

scanner.close();
}
}
Output:-
Program :- 8
Code:-
package mohit;
class Room{
int roomno;
String roomtype;
double roomarea;
int ACmachine;

public void setData(int roomno, String roomtype, double roomarea, int


ACmachine) {
this.roomno= roomno;
this.roomtype=roomtype;
this.roomarea= roomarea;
this.ACmachine= ACmachine;
}
public void displayData() {
System.out.println("myroomno: " + roomno);
System.out.println("myroomtype: " + roomtype);
System.out.println("myroomarea: " + roomarea);
System.out.println("myACmachine: " + ACmachine);
}

public class Program8 {

public static void main(String[] args) {


// TODO Auto-generated method stub
Room obj=new Room();
obj.setData(10,"hello",6.34,50);
obj.displayData();
}
}

Output:-
Program:-9
Code:-
package mohit;
class Shape {
public void draw() {

public void erase() {

}
}

class Circle extends Shape{

public void draw() {


System.out.println("CLASS CIRCLE ");
System.out.println("draw circle ");

}
public void erase() {System.out.println("CLASS CIRCLE ");
System.out.println(" erase circle ");
}

}
class Triangle extends Shape{

public void draw() {


System.out.println("CLASS TRAINGLE");

System.out.println("draw triangle ");


}
public void erase() {
System.out.println("CLASS TRAINGLE ");
System.out.println(" erase triangle ");
}

}
class Square extends Shape{

public void draw() {


System.out.println("CLASS SQUARE ");
System.out.println("draw square");
}
public void erase() {
System.out.println("CLASS SQUARE ");
System.out.println(" erase square ");
}
}

public class Program9 {

public static void main(String[] args) {

// TODO Auto-generated method stub


Shape circle = new Circle ();
Shape triangle = new Triangle();
Shape square = new Square();

circle.draw();
circle.erase();
square.draw();
square.erase();
triangle.draw();
triangle.erase();

Ouput:-

You might also like