You are on page 1of 10

Student ID : 202112052

IT 602: Object-Oriented Programming Lab


Assignment 1: Declarations - Classes and Arrays

Question #1:
You are supposed to create a class for vehicles. In the class, you should include at least two private members
named no_of_seats and no_of_wheels. You should have methods for getters and setters for the required private
members. In the driver code, you have to create two objects of the same class. (i.e., one for Motorcycle and one
for Class). Your output should show the descriptions for Car and Motorcycle. Also, output the size occupied by the
objects in the memory.

Code #1:
package com;
import java.util.*;

//create a class for vehicles


class Vehicles {
/*two private members*/
private int no_of_seats;
private int no_of_wheels;

/*getter method for no_of_seats*/


public int getNo_of_seats() {
return no_of_seats;
}
/*setter method for no_of_seats*/
public void setNo_of_seats(int no_of_seats) {
this.no_of_seats = no_of_seats;
}
/*getter method for no_of_wheels*/
public int getNo_of_wheels() {
return no_of_wheels;
}
/*setter method for no_of_wheels*/
public void setNo_of_wheels(int no_of_wheels) {
this.no_of_wheels = no_of_wheels;
}
}

//Driver Code
public class Q1 {
public static void main(String[] args) {
Vehicles motorcycle = new Vehicles();
Vehicles car = new Vehicles();
Scanner sc = new Scanner(System.in);

//Take User Input


System.out.print("Enter No. of Seats in Motorcycle: ");
int m1 = sc.nextInt();
motorcycle.setNo_of_seats(m1);

System.out.print("Enter No. of Wheels in Motorcycle: ");


int m2 = sc.nextInt();
motorcycle.setNo_of_wheels(m2);
Student ID : 202112052

System.out.print("Enter No. of Seats in Car: ");


int c1 = sc.nextInt();
car.setNo_of_seats(c1);

System.out.print("Enter No. of Wheels in Car: ");


int c2 = sc.nextInt();
car.setNo_of_wheels(c2);

System.out.println("\nMotorcycle -> Seats:" + motorcycle.getNo_of_seats() + "


Wheels:" + motorcycle.getNo_of_wheels());
System.out.println("Car -> Seats:" + car.getNo_of_seats() + " Wheels:" +
car.getNo_of_wheels());

}
}

Output #1:

Observations/Remarks #1:

Question #2:
You have to design a class for the calendar. Your class should include basic information related to the calendar. In
this, you are allowed to create only one public method, which will decide whether a given year is a leap year or
not. Design a constructor through which you can assign the values to your private members. For the output, create
three different objects of the calendar for three different years. Output whether the given years are leap years or
not. Note: A year is a leap year if, - It has an extra day i.e. 366 instead of 365 - It occurs every 4 years e.g. 2008,
2012 are leap years - For every 100 years, a special rule applies-1900 is not a leap year but 2000 is a leap year. In
those cases, we need to check whether it is divisible by 400 or not.

Code #2:
package com;
//create a class for calender
class Calender{
private int year;
Calender(int y){
this.year=y;
}
void checkLeapYear(){
// if the year is divided by 4
if(this.year%4==0){
// if the year is century
Student ID : 202112052

if (this.year%100==0){
// if year is divided by 400
// then it is a leap year
if (this.year%400==0)
System.out.println(this.year + " is leap year");
else
System.out.println(this.year + " is not leap year");
}
else
System.out.println(this.year + " is leap year");
}
else
System.out.println(this.year + " is not leap year");
}
}
//Driver Code
public class Q2 {
public static void main(String[] args) {
Calender y1 = new Calender(2012);
y1.checkLeapYear();

Calender y2 = new Calender(2000);


y2.checkLeapYear();

Calender y3 = new Calender(1900);


y3.checkLeapYear();
}
}

Output #2:

Observations/Remarks #2:

Question #3:
You are supposed to create a calculator class. In this class, you can create methods for addition, subtraction, and
multiplication. Your methods should have support for both integer values and float values. Method names must be
the same for the respective tasks. Create overloading methods for doing this. You should have at least two
overloaded methods. You can assign values to your private variables via different constructors. (i.e. One for integer
values and one for the float values)
Student ID : 202112052

Code #3:
package com;
//create a class for calculator
class Calculator{
private double n1,n2;
private boolean b;
//constructor for float value
Calculator(double x, double y){
this.n1=x;
this.n2=y;
b=false;
}
//constructor for int value
Calculator(int x, int y){
this.n1=x;
this.n2=y;
b=true;
}
//overloading methods
double addition(double x, double y){
return x+y;
}
int addition(int x, int y){
return x+y;
}
double subtraction(double x, double y){
return x-y;
}
int subtraction(int x, int y){
return x-y;
}
double multiplication(double x, double y){
return x*y;
}
int multiplication(int x, int y){
return x*y;
}

/**call methods first check if input value is integer then execute if condition
otherwise consider as float value and execute else condition*/
void addition(){
if (b)
System.out.println("Addition : " + addition((int)this.n1,(int)this.n2));
else
System.out.println("Addition : " + addition(this.n1, this.n2));
}
void subtraction(){
if (b)
System.out.println("Subtraction : " + subtraction((int)this.n1,
(int)this.n2));
else
System.out.println("Subtraction : " + subtraction(this.n1, this.n2));
}
void multiplication(){
if (b)
System.out.println("Multiplication : " + multiplication((int)this.n1,
(int)this.n2));
else
System.out.println("Multiplication : " + multiplication(this.n1,
this.n2));
}
//check input value is integer or float and then print calculation
Student ID : 202112052

void display()
{
if(b)
System.out.println("\nCalculations of "+(int)this.n1+" and "+(int)this.n2
+ " is:");
else
System.out.println("\nCalculations of "+this.n1+" and "+this.n2 + " is:");
}
}
public class Q3 {
public static void main(String[] args) {
Calculator c1 = new Calculator(10,12);
Calculator c2 = new Calculator(10.5,12.5);

c1.display();
c1.addition();
c1.subtraction();
c1.multiplication();

c2.display();
c2.addition();
c2.subtraction();
c2.multiplication();
}
}

Output #3:

Observations/Remarks #3:

Question #4:
You are in a locker room with 100 open lockers, numbered 1 to 100. Toggle all of the lockers that are even. By
toggle, we mean close if it is open, and open if it is closed. Now toggle all of the lockers that are multiples of three.
Repeat with multiples of 4, 5, up to 100. Output the number of lockers that will be open after the simulation.
Student ID : 202112052

Code #4:
package com;
public class Q4 {
public static void main(String[] args) {
boolean[] lockers = new boolean[100];
int count=0;
//open all multiples of 1 locker
for (int i = 1; i<=100; i++){
lockers[i-1]=true;
}
//open every locker for multiply of i
for (int i=1; i<=100; i++){
for (int j=2; j<=i/2; j++){
if (i % j==0) {
lockers[i - 1] = !lockers[i - 1]; //toggle
}
}
if (i>=2) {
lockers[i - 1] = !lockers[i - 1];
}
if (lockers[i - 1]){
System.out.print(i+ " ");
count++;
}
}
System.out.print("\nTotal Open Lockers are : " + count);
}
}

Output #4:

Observations/Remarks #4:

Question #5:
Write a Java program that prints all real solutions to the quadratic equation ax2+ bx + c = 0. Read in a, b, c and use
the quadratic formula. Before finding the solution, you should make a check for the real solutions. Find the value of
the discriminant (i.e. b 2 - 4ac). If the discriminant value is negative, display a message stating that there are no
real solutions. To implement this solution, make an appropriate class and appropriate methods.

Code #5:
Student ID : 202112052

package com;
import java.util.Scanner;

class QuadraticEquation {
void calculateRoots(double a, double b, double c) {
//if a is 0 then the equation is not quadratic
if (a == 0) {
System.out.println("The value of a cannot be 0");
return;
}
// calculate the determinant (b2 - 4ac)
double root1, root2;
double determinant = b * b - 4 * a * c;
System.out.println("Value of the Discriminant is: "+determinant);

// check if determinant is greater than 0


if (determinant > 0) {
// two real and distinct roots
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.println("\nThe roots of the equation are real and different: \
n"+root1+"\n"+root2);
}
// check if determinant is equal to 0
else if (determinant == 0) {
// two real and equal roots
// determinant is equal to 0
root1 = root2 = -b / (2 * a);
System.out.println("\nThe roots of the equation are real and same: \
n"+root1+"\n"+root2);
}
// if determinant is less than zero
else {
// roots are complex number and distinct
System.out.println("there are no real solutions");
}
}
}
public class Q5 {
public static void main(String[] args) {
QuadraticEquation qe = new QuadraticEquation();
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of a: ");
double a = sc.nextDouble();

System.out.print("Enter value of b: ");


double b = sc.nextDouble();

System.out.print("Enter value of c: ");


double c = sc.nextDouble();
//calling function
qe.calculateRoots(a, b, c);
}
}

Output #5:
Student ID : 202112052

Observations/Remarks #5:

Question #6:
The program needs to calculate the nth power of a matrix whose elements, as well as the value of n, are specified
by the user. You should store your matrix in the 2D Array. You must create supporting methods for doing matrix
multiplications.

Code #6:
package com;

import java.util.Scanner;

class Matrix{
private int[][] matrix;
private int row,col;
//store matrix in 2D Array
Matrix(int row,int col){
this.row=row;
this.col=col;
matrix=new int[row][col];
}
//set Matrix value
public void setMatrix(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Matrix Values: ");
for (int i=0; i<row; i++){
for (int j=0; j<col; j++){
matrix[i][j]=sc.nextInt();
}
}
}
//calculation nth power of matrix
public void power(int p){
for (int i=2; i<=p; i++){
int[][] temp=new int[this.row][this.col];
for (int j=0; j<this.row;j++){
for (int k=0; k<this.col;k++){
temp[j][k]=0;
Student ID : 202112052

for (int m=0;m<this.col;m++){


temp[j][k]+=this.matrix[j][m]*this.matrix[m][k];
}
}
}
this.matrix=temp;
}
}
//print matrix
public void display(){
for (int i=0; i<this.row;i++){
for (int j=0; j<this.col; j++){
System.out.print(this.matrix[i][j]+" ");
}
System.out.println();
}
}
}
//Driver Code
public class Q6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row,col;

System.out.print("Enter Row for 2D Matrix: ");


row=sc.nextInt();

System.out.print("Enter Row for 2D Matrix: ");


col=sc.nextInt();
//check whether given matrix value is square matrix or not
if (row==col){
Matrix m = new Matrix(row,col);
m.setMatrix();
System.out.print("Enter Power for 2D Matrix: ");
int pwr= sc.nextInt();
m.power(pwr);
m.display();
}
else {
System.out.println("Matrix is not Square Matrix");
}
}
}

Output #6:
Student ID : 202112052

Observations/Remarks #6:

You might also like