0% found this document useful (0 votes)
153 views16 pages

Object Oriented Programming Assignment Aman Paniya MBA Tech LL Year

The document contains the solutions to 10 questions on Object Oriented Programming concepts in Java. It includes programs to find the largest of two numbers, calculate max, min and sum of three numbers, calculate area of shapes using switch case, print patterns using loops, demonstrate break and continue statements, use for-each loops, constructors, this and super keywords, final keyword, and create a GUI using Swing components. The programs contain the code solutions and output for each question.

Uploaded by

Aman Paniya
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)
153 views16 pages

Object Oriented Programming Assignment Aman Paniya MBA Tech LL Year

The document contains the solutions to 10 questions on Object Oriented Programming concepts in Java. It includes programs to find the largest of two numbers, calculate max, min and sum of three numbers, calculate area of shapes using switch case, print patterns using loops, demonstrate break and continue statements, use for-each loops, constructors, this and super keywords, final keyword, and create a GUI using Swing components. The programs contain the code solutions and output for each question.

Uploaded by

Aman Paniya
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/ 16

Object Oriented Programming Assignment

Aman Paniya
MBA Tech ll Year

Q1) Write a Java program to check largest amongst two no.


(Use if-else).
import java.util.*;

public class e1q1

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter first number");

int x = sc.nextInt();

System.out.println("Enter second number");

int y = sc.nextInt();

if(x>y)

System.out.println(x+" is greater");

else

System.out.println(y+" is greater");

Output
Enter first number 12

Enter the second number 13

13 is greater

Q2) WAP to accept 3 nos. From command line arguments


and print maximum, minimum and total of all three nos.
public class e1q2

public static void main(String args[])

int x = Integer.parseInt(args[0]);

int y = Integer.parseInt(args[1]);

int z = Integer.parseInt(args[2]);

if(x>y&&x>z)

System.out.println(x+" is maximum");

else if(y>x&&y>z)

System.out.println(y+" is maximum");

else

System.out.println(z+" is maximum");

if(x<y&&x<z)

System.out.println(x+" is minimum");

else if(y<x&&y<z)

System.out.println(y+" is minimum");

else
System.out.println(z+" is minimum");

System.out.println("Total = "+(x+y+z)) }}

Output write the three no 10 20 30

Maximum 30

Minimum 10

Sum 60

Q3) Write a menu driven java program to find area of circle


and triangle [make use of switch case].
import java.util.Scanner;

public class e1q3

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter 1 for area of traingle\nEnter 2 for area of


circle");

int choice = sc.nextInt();

switch(choice)

case 1:
System.out.println("Enter the base of traingle");

int b = sc.nextInt();

System.out.println("Enter the height of traingle");

int h = sc.nextInt();

System.out.println("Area of traingle = "+(0.5*b*h) + " square units");

break;

case 2:

System.out.println("Enter the radius of circle");

int r = sc.nextInt();

System.out.println("Area of circle = "+(3.14*r*r) + " square units");

break;

default:

System.out.println("Wrong choice");}}}

Output

1. Area of circle
2. Area of triangle
Enter your choice: 1
Enter radius: 3
Area = 28.274333882308138

Q4) Write program to print following pattern


*
**
***
****
*****
import java.util.Scanner;

public class e1q4

public static void main(String args[])

Scanner sc = new Scanner(System.in);

int size = 0;

System.out.println("Enter height of the triangle : ");

size = sc.nextInt();

int i, j, k;

for (i = 0; i < size + 1; i++)

for (j = size; j > i; j--)

System.out.print(" ");

for (k = 0; k < (2 * i - 1); k++)

System.out.print("*");

System.out.println();

}
}

Output

**

***

****

*****

Q5) Write a program to illustrate break and continue (two


separate programs )

public class e1q5p1

public static void main(String args[])

int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers )

if( x == 30 )
{

break;

System.out.print( x );

System.out.print("\n");

Output

10

20

public class e1q5p2

public static void main(String args[])

int [] numbers = {10, 20, 30, 40, 50};

for(int x : numbers )

if( x == 30 )

{
continue;

System.out.print( x );

System.out.print("\n");

Output

10

20

30

40

50

Q6) Write a program to implement for each (Extended for


loop) loop using example.
public class e1q6

public static void main(String[] arg)

{
{

int[] marks = { 125, 132, 95, 116, 110 };

int highest_marks = maximum(marks);

System.out.println("The highest score is " + highest_marks);

public static int maximum(int[] numbers)

int maxSoFar = numbers[0];

for (int num : numbers)

if (num > maxSoFar)

maxSoFar = num;

return maxSoFar; } }

output -

the highest score is 132


Q7) Create a Class Student with members name and roll no.
Write Java Program to initialize roll no. and name using
default and parameterized constructor and print them.
import java.util.Scanner;

public class Student

static void Student(String name,int rn)

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

System.out.println("Roll No. : "+rn);

public static void main(String args[])

Scanner sc = new Scanner(System.in);

System.out.println("Enter the name os the student");

String name = sc.next();

System.out.println("Enter the roll no. of the student");

int rn = sc.nextInt();

Student(name,rn); }}

Output

Enter the name of the student Aman

Enter the roll no of student 12

Name : Aman

Roll no :12
Q8) Write a program to implement this keyword to refer to
current class instance variable.
public class Account

int a;

int b;

public void setData(int a ,int b)

this.a = a;

this.b = b;

public void showData()

System.out.println("Value of A ="+a);

System.out.println("Value of B ="+b);

public static void main(String args[])

Account obj = new Account();

obj.setData(2,3);

obj.showData();}}
Output

Value of A =2

Value of B =3

Q9) Write a program to implement super keyword to refer


immediate parent class instance variable.
class TestSuper1

public static void main(String args[])

Dog d=new Dog();

d.printColor();

class Animal

String color="white";

class Dog extends Animal

String color="black";

void printColor()

{
System.out.println(color);//prints color of Dog class

System.out.println(super.color);//prints color of Animal class }}

Output

black

white

Q10) Write a program to implement final keyword.

public class finalkey

public static void main(String[] args)

final int hours=24;

System.out.println("Hours in 6 days = " + hours * 6);

Output

Hours in 6 days=144

Q11) Write a Java Program to implement GUI.


import java.awt.FlowLayout;

import javax.swing.*;
public class JFrameExample {

public static void main(String s[]) {

JFrame frame = new JFrame("GUI in Java");

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout());

JLabel label = new JLabel("GUI in Java");

JTextField t1;

t1=new JTextField("Welcome to Java");

t1.setBounds(50,100, 200,30);

frame.add(t1);

frame.setSize(400,400);

JButton button = new JButton();

button.setText("Click Here");

JCheckBox checkBox1 = new JCheckBox("C++");

checkBox1.setBounds(100,200,50,50);

JCheckBox checkBox2 = new JCheckBox("Java", true);

checkBox2.setBounds(100,150, 100,50);

JRadioButton r1=new JRadioButton("A) Male");

JRadioButton r2=new JRadioButton("B) Female");

r1.setBounds(300,150,100,30);

r2.setBounds(300,200,100,30);

ButtonGroup bg=new ButtonGroup();

bg.add(r1);bg.add(r2);
frame.add(r1);frame.add(r2);

frame.add(checkBox1);

frame.add(checkBox2);

DefaultListModel<String> l1 = new DefaultListModel<>();

l1.addElement("Item1");

l1.addElement("Item2");

l1.addElement("Item3");

l1.addElement("Item4");

JList<String> list = new JList<>(l1);

list.setBounds(100,250, 75,75);

String country[]={"India","Aus","U.S.A","England","Newzealand"};

JComboBox cb=new JComboBox(country);

cb.setBounds(300, 300,90,20);

frame.add(cb);

frame.add(list);

panel.add(label);

panel.add(button);

frame.add(panel);

frame.setLocationRelativeTo(null);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

You might also like