0% found this document useful (0 votes)
23 views10 pages

OOPS

Uploaded by

Ashmita Bose
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)
23 views10 pages

OOPS

Uploaded by

Ashmita Bose
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/ 10

OBJECT ORIENTED PROGRAMMING

Name: Soumili Mondal


Stream: CSE
Year: 3rd Year
Sem: 5th Sem
Roll: 24400120031
Subject: Object Oriented Programming
INDEX

Sl. Problem statement Date Remarks Teacher


No. sign

1 Write program to find sum


of 2 numbers

2 Write a program to find the


greater of 2 numbers

3 Write a program to find the


greater of 3 numbers

4 Write a program to find the


greatest of 10 numbers

5 Write a program to show


the pattern

6 Write a program to show


inheritance in OOP

7 Write a program to show


distance in feet and inches
using Constructor
overloading

8 Write a program to show


time in hours, minutes,
seconds
Problem Statement 1:
Write a program to find the sum of two numbers.

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
int x, y, sum;
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
sum= x + y;
System.out.println("The sum is "+sum);
}
}

Input/Output:
12
24
The sum is 36

Problem Statement 2:
Write a program to find the greater of two numbers.

Code :
import java.util.*;
public class Main
{
public static void main (String[]args)
{
int x, y;
Scanner sc = new Scanner (System.in);
x = sc.nextInt ();
y = sc.nextInt ();
System.out.println ("The greater is: ");
if (x > y){
System.out.println (x);
}else{
System.out.println (y);
}
}
}

Input/Output:
100
99
The greater is:
100

Problem Statement 3:
Write a program to find the greater of three numbers.

Code:
import java.util.*;
public class Main
{
public static void main (String[]args)
{
int x, y, z;
Scanner sc = new Scanner (System.in);
x = sc.nextInt ();
y = sc.nextInt ();
z = sc.nextInt ();
System.out.println ("The greater is: ");
if (x > y && x > z){
System.out.println (x);
}
else if (y > z){
System.out.println (y);
}
else{
System.out.println (z);
}
}
}

Input/Output:
50 99 10
The greater is:
99

Problem Statement 4:
Write a program to find the greatest of 10 numbers.

Code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int [] arr= new int[10];
for(int i=0; i<10; i++){
arr[i]=sc.nextInt();
}
int maxe=arr[0];
for(int i=1; i<10; i++){
if(arr[i]>maxe){
maxe=arr[i];
}
}
System.out.println("The greatest number is "+maxe);
}
}

Output:
98 100 12 5 8 35 76 82 90 10
The greatest number is 100

Problem Statement 5:
Write a program to show the given pattern.
*
**
***
****
Code :
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rows;
rows=sc.nextInt();
for(int i=0; i<rows; i++){
for(int j=0; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Output:
5
*
**
***
****
*****

Problem Statement 6:
Write a program to show Inheritance in OOP.

Code:
import java.util.*;
class animal{ //base class
public String food;
public String habitat;
public animal(String f, String h){
this.food=f;
this.habitat=h;
}
}
class pet extends animal{
public int lifespan;
public pet(int l, String f, String h){
super(f,h);
this.lifespan=l;
}
public String toString(){
return ("Your pet loves to eat "+food+". Average lifespan is
"+lifespan+". Its habitat is "+habitat);
}
}
public class Main
{
public static void main(String[] args) {
pet cat=new pet(15,"fish","land");
System.out.println(cat.toString());
}
}

Output:
Your pet loves to eat fish. Average lifespan is 15. Its habitat is land

Problem Statement 7:
Write a program to show distance in feet and inches
using Constructor overloading.
Code:
import java.util.*;
class distance{
public int feet,inches;
public distance(){
feet=0;
inches=0;
System.out.println("The distance is 0");
}
public distance(int f){
feet=f;
inches=0;
System.out.println("The distance (in feet) is "+feet);
}
public distance(int f, int i){
feet=f;
inches=i;
int distance=feet*12+inches;
System.out.println("The distance (in inch) is "+distance);
}
}
public class Main
{
public static void main(String[] args) {
distance a= new distance();
distance b= new distance(10);
distance c= new distance(10, 12);

}
}

Output:
The distance is 0
The distance (in feet) is 10
The distance (in inch) is 132

Problem Statement 8:
Write a program to show time in hours, minutes and
seconds.
Code:
import java.util.*;
class time{
public int hour,min,sec;
public time(int t){
hour=t/3600;
t%=3600;
min=t/60;
t%=60;
sec=t;
System.out.println("Time is "+hour+" Hour "+min+" Minutes
"+sec+" Seconds ");
}
}
public class Main
{
public static void main(String[] args) {
time t=new time(6030);
}
}

Output:

Time is 1 Hour 40 Minutes 30 Seconds

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy