[go: up one dir, main page]

0% found this document useful (0 votes)
10 views8 pages

Mark 1

Uploaded by

anuharshpawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Mark 1

Uploaded by

anuharshpawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

NAME : Anuharsh Pawar

UID : 2022300004

Practice-Set I - Encapsulation: Objects & Classes, Constructors

QUE 1 : Write a program to find the sum of the first two digit of two numbers. Use the
concept of classes and objects.

Code :

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter the first number: ");


int num1 = input.nextInt();

System.out.print("Enter the second number: ");


int num2 = input.nextInt();

NumberAdder adder = new NumberAdder(num1, num2);


int sum = adder.addFirstTwoDigits();

System.out.println("The sum of the first two digits of the two numbers is: " + sum); }
}

class NumberAdder {
private int num1;
private int num2;

public NumberAdder(int num1, int num2) {


this.num1 = num1;
this.num2 = num2;
}

public int addFirstTwoDigits() {


int firstTwoDigitsOfNum1 = Integer.parseInt(Integer.toString(num1).substring(0, 2));
int firstTwoDigitsOfNum2 = Integer.parseInt(Integer.toString(num2).substring(0, 2));
return firstTwoDigitsOfNum1 + firstTwoDigitsOfNum2;
}
}
Output :

Que 2 : Create class Product. Using a method, set its values for product name, prize, brand.
Write
another method to display the GST needed to pay on the current object.

Code :
class Product {
private String name;
private double price;
private String brand;
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setBrand(String brand) {
this.brand = brand;
}
public void displayGST() {
double gst = price * 0.25;
System.out.println("GST on " + name + " (" + brand + ") is " + gst);
}
}
public class Main {
public static void main(String[] args) {
Product product = new Product();
product.setName("Laptop");
product.setBrand("HP");
product.setPrice(485990);
product.displayGST();
}
}
Output :
Que 3 : Write a program to design a Class cycler with name, age, no. of marathon attended
and calculate the average speed of each cycler. use appropriate data for cycler if missing.

Code :

class Cycler {
String name;
int age;
int noOfMarathonsAttended;

public Cycler(String name, int age, int noOfMarathonsAttended) {


this.name = name;
this.age = age;
this.noOfMarathonsAttended = noOfMarathonsAttended;
}

public void setName(String name) {


this.name = name;
}

public void setAge(int age) {


this.age = age;
}

public void setNoOfMarathonsAttended(int noOfMarathonsAttended) {


this.noOfMarathonsAttended = noOfMarathonsAttended;
}

public double calculateAverageSpeed(double distanceCovered, double timeTaken)


{ return distanceCovered / timeTaken;
}
}
public class Main {
public static void main(String[] args) {
Cycler cycler = new Cycler("EREN YEAGER", 45, 20);
double distanceCovered = 80// Distance covered in a marathon (in km) double
timeTaken = 4; // Time taken to complete the marathon (in hours) double
averageSpeed = cycler.calculateAverageSpeed(distanceCovered, timeTaken);
System.out.println(cycler.name + " (age: " + cycler.age + ") has attended " +
cycler.noOfMarathonsAttended + " marathons and has an average speed of " +
averageSpeed + " km/h.");
}
}

Output :

QUE 4 : A painter is given a contract to paint a wall. He goes and measures with a meter
tape. However
he makes further calculations in foot. Cost of painting per square foot is Rs. 10. Find the
amount
to be paid to him for painting. A constructor is used to set the length and breadth of the wall.
Use an object as a parameter. Note: 1 meter= 3.28 feet.

Code :

class Painter {

private double lengthInMeters;

private double breadthInMeters;

public Painter(double lengthInMeters, double breadthInMeters) {

this.lengthInMeters = lengthInMeters;

this.breadthInMeters = breadthInMeters;

}
public double calculateAreaInSquareFeet() {

double lengthInFeet = lengthInMeters * 23.9;

double breadthInFeet = breadthInMeters * 23.9;

double areaInSquareFeet = lengthInFeet * breadthInFeet;

return areaInSquareFeet;

public double calculateAmountToBePaid(double costPerSquareFoot) {

double areaInSquareFeet = calculateAreaInSquareFeet();

double amountToBePaid = areaInSquareFeet * costPerSquareFoot;

return amountToBePaid;

public class Main {

public static void main(String[] args) {

Painter painter = new Painter(8, 5);


double costPerSquareFoot = 17;

double amountToBePaid = painter.calculateAmountToBePaid(costPerSquareFoot);


System.out.println("The amount to be paid to the painter is Rs. " + amountToBePaid); }

Output :

Que 5 : Create a Class Student containing the student's name, marks of 5 papers of a
subject (use integer
array).
Use parameterized constructors to initialize values of marks and
name. Write a method:
1. To find average of 5 papers
2. To display the name of the student, marks of 5 papers, and average.

Code :

class Student {

private String name;

private int[] marks;

public Student(String name, int[] marks) {

this.name = name;
this.marks = marks;

public double calculateAverage() {

double sum = 0;

for (int i = 0; i < marks.length; i++) {

sum += marks[i];

double average = sum / marks.length;

return average;

public void displayDetails() {

System.out.println("Name: " + name);

System.out.print("Marks: ");

for (int i = 0; i < marks.length; i++) {

System.out.print(marks[i] + " ");


}

System.out.println();

System.out.println("Average: " + calculateAverage()); }

public class Main {

public static void main(String[] args) {

int[] marks = { 99, 100, 100, 99, 98 };

Student student = new Student("Mikasa Ackermann", marks); student.displayDetails();

} Ouput :

You might also like