OOPS
OOPS
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: