0% found this document useful (0 votes)
6 views9 pages

CS231173

Uploaded by

hiraazhar2030
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)
6 views9 pages

CS231173

Uploaded by

hiraazhar2030
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/ 9

1

STUDENT REG NO: CS231173


STUDENT NAME: Hira Azher

Q1:
import java.util.Scanner;
class Prime {
private int number;
public void setNumber(int
number) {
this.number = number;
}
public int getNumber() {
return number;
}
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 int sumUpToNumber() {
int sum = 0;
for (int i = 1; i <= number; i++) {
2

sum += i;
}
return sum;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Prime primeObj = new Prime();
System.out.print("Enter an integer: ");
int inputNumber = sc.nextInt();
primeObj.setNumber(inputNumber);
if (primeObj.isPrime()) {
System.out.println(inputNumber + " is a prime number.");
} else {
System.out.println(inputNumber + " is not a prime number.");
}
int sum = primeObj.sumUpToNumber();
System.out.println("The sum of all integers from 1 to " + inputNumber + " is:
" + sum);
}
}
3

Q2)
class Student {
private String name;
private int age;
private char grade;
public Student(String name, int age, char
grade) {
this.name = name;
this.age = age;
this.grade = grade;
}
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Grade: " + grade);
}
}
public class StudentDemo {
public static void main(String[] args) {
Student student1 = new Student("Muddassir", 36, 'A');
Student student2 = new Student("Hira", 23, 'B');
student1.displayDetails();
System.out.println();
student2.displayDetails();
}
}
4

Q3
class MathUtils {
public static int findMax(int x, int y, int z) {
int largest = (x > y) ? x : y;
largest = (z > largest) ? z : largest;
return largest;
}
public static double computeAverage(int[] values) {
int total = 0;
for (int value : values) {
total += value;
}
return values.length > 0 ? (double) total / values.length : 0;
}
}
public class Main {
public static void main(String[] args) {
int largestValue = MathUtils.findMax(18, 5, 24);
System.out.println("Max of 18, 5, and 24: " + largestValue);
int[] arrayNumbers = {12, 22, 32, 42, 52};
double averageValue = MathUtils.computeAverage(arrayNumbers);
System.out.println("Average of array: " + averageValue);
}
}
5

Q4)
abstract class Appliance {
public abstract void turnOn();
}
class Fan extends Appliance {
public void turnOn() {
System.out.println("Fan is now running");
}
}
class Light extends Appliance {
public void turnOn() {
System.out.println("Light is now switched on");
}
}
public class ApplianceDemo {
public static void main(String[] args) {
Fan fan = new Fan();
Light light = new Light();
fan.turnOn();
light.turnOn();
}
}
6

Q5)
import java.util.Scanner;
class Shape {
public double area() {
return 0.0;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double area() {
return length * width;
7

}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the radius of the circle: ");
double radius = sc.nextDouble();
Shape circle = new Circle(radius);
System.out.println("Area of the circle: " + circle.area());
System.out.print("Enter the length of the rectangle: ");
double length = sc.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = sc.nextDouble();
Shape rectangle = new Rectangle(length, width);
System.out.println("Area of the rectangle: " + rectangle.area());
}
}

Q6)
class Address {
private String street;
private String city;
private String zipcode;
public Address(String street, String city, String zipcode) {
this.street = street;
this.city = city;
8

this.zipcode = zipcode;
}
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getZipcode() {
return zipcode;
}
}
class Person {
private String name;
private int age;
private Address address;

public Person(String name, int age, Address address) {


this.name = name;
this.age = age;
this.address = address;
}
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Address:");
9

System.out.println("Street: " + address.getStreet());


System.out.println("City: " + address.getCity());
System.out.println("Zipcode: " + address.getZipcode());
}
}
public class PersonDemo {
public static void main(String[] args) {
Address personAddress = new Address("B-15 Defence view Main Street",
"kashmir Road", "75500");
Person person = new Person("Hira", 24, personAddress);
person.displayDetails();
}
}

You might also like