[go: up one dir, main page]

0% found this document useful (0 votes)
6 views31 pages

FOINAL LAB MANUAL

The document contains a series of Java programs covering various topics in Object-Oriented Programming, including sequential programming, conditional structures, arrays, encapsulation, inheritance, polymorphism, and graphical user interfaces. Each section provides multiple code examples demonstrating the implementation of these concepts. The programs include functionalities such as arithmetic operations, grade calculations, and area calculations for different shapes.
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)
6 views31 pages

FOINAL LAB MANUAL

The document contains a series of Java programs covering various topics in Object-Oriented Programming, including sequential programming, conditional structures, arrays, encapsulation, inheritance, polymorphism, and graphical user interfaces. Each section provides multiple code examples demonstrating the implementation of these concepts. The programs include functionalities such as arithmetic operations, grade calculations, and area calculations for different shapes.
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/ 31

SUBMITTED BY :

AWAIS KAMEER
SUBMITTED TO :
TEACHER FATIMA
SUBJECT :
OBJECT ORIENTED PROGRAMMING
ROLL NO :
70167676
LAB MANUAL :
OOP PROGRAMS

THE UNIVERSITY OF LAHORE


(SARGODHA CAMPUS)
TOPIC : SEQUENTIAL PROGRAMMING
1)PROGRAM
package javaapplication16;
class Stud{
int id;
String name;
int marks;
void output(){
System.out.println("RESULTS is given below");
}
void input(int a,String n,int b){
id=a;
name=n;
marks=b;
}
void finaloutput(){
System.out.println(id+" "+name+" "+marks);
}
}
public class awais{
public static void main(String[] args){
Stud old=new Stud();
old.output();
old.input(10,"AWAIS",100);
old.finaloutput();
}
}
2) PROGRAM
package javaapplication21;
import java.util.*;
class JavaApplication21 {
public void subtraction(int x,int y){
System.out.println("result is = "+(x-y));
}
public static void main(String[] args){
int x;
int y;
Scanner input=new Scanner(System.in);
System.out.println("plz enter value of x and y");
x=input.nextInt();
y=input.nextInt();
JavaApplication21 obj=new JavaApplication21();
obj.subtraction(x, y);
}
}

3)PROGRAM
package javaapplication23;
import java.util.*;
public class JavaApplication23 {
public void sum(int x,int y,int z){
System.out.println(x+y+z);
}
public static void main(String[] args) {
int x;
int y;
int z;
Scanner input=new Scanner(System.in);
x=input.nextInt();
y=input.nextInt();
z=input.nextInt();
JavaApplication23 obj=new JavaApplication23 ();
obj.sum(x, y, z);
}
}

4)PROGRAM
package javaapplication23;
import java.util.*;
public class JavaApplication23 {
public void MUL(int x,int y,int z){
System.out.print("result = ");
System.out.println(x*y*z);
}
public static void main(String[] args) {
int x;
int y;
int z;
System.out.println("please enter the values");
Scanner input=new Scanner(System.in);
x=input.nextInt();
y=input.nextInt();
z=input.nextInt();
JavaApplication23 obj=new JavaApplication23 ();
obj.MUL(x, y, z);
}
}

TOPIC : Conditional Structure


1)program
package javaapplication27;
import java.util.*;
public class JavaApplication27 {
protected int a;
public void set(int x){
a=x;
}
public void even(){
if(a%2==0){
System.out.println("even");
}
else{
System.out.println("odd");
}
}
public static void main(String args[]){
int a;
Scanner in=new Scanner(System.in);
System.out.println("please enter the value");
a=in.nextInt();
JavaApplication27 obj=new JavaApplication27();
obj.set( a);
obj.even();
}
}

2)program
package javaapplication27;
import java.util.*;
public class JavaApplication27 {
protected int a;
public void set(int x){
a=x;
}
public void even(){
if(a>=0){
System.out.println("posoitive");
}
else{
System.out.println("negative");
}
}
public static void main(String args[]){
int a;
Scanner in=new Scanner(System.in);
System.out.println("please enter the value");
a=in.nextInt();
JavaApplication27 obj=new JavaApplication27();
obj.set( a);
obj.even();
}
}

3)program
package javaapplication27;
import java.util.*;
public class JavaApplication27 {
protected int a;
public void set(int x){
a=x;
}
public void GRADES(){
if(a>=90&&a<=100){
System.out.println("A GRADE");
}
else if(a>=80&&a<90){
System.out.println("B GRADE");
}
else if(a>=70&&a<80){
System.out.println("C GRADE");
}
else if(a>=60&&a<70){
System.out.println("D GRADE");
}
else if(a>=50&&a<60){
System.out.println("E GRADE");
}
else{
System.out.println("FAIL");
}
}
public static void main(String args[]){
int a;
Scanner in=new Scanner(System.in);
System.out.println("please enter the MARKS");
a=in.nextInt();
JavaApplication27 obj=new JavaApplication27();
obj.set( a);
obj.GRADES();
}
}

4)program

package javaapplication45;
import java.util.*;
public class JavaApplication45 {
public void day(char b){
switch(b){
case'a':{
System.out.println("monday");
break;
}
case'b':{
System.out.println("tuesday");
break;
}
case'c':{
System.out.println("wednesday");
break;
}
case'd':{
System.out.println("thursday");
break;
}
case'e':{
System.out.println("friday");
break;
}
case'f':{
System.out.println("saturday");
break;
}
case'g':{
System.out.println("sunday");
break;
}
default:{
System.out.println("you entered incorrect character");
}
}
}
public static void main(String[] args) {
char x;
System.out.println("plzz enter character");
Scanner in=new Scanner(System.in);
x=in.next().charAt(0);
JavaApplication45 obj=new JavaApplication45 ();
obj.day(x);
}
}

Topic : 1d ARRAY
1)program

package javaapplication46;
import java.util.*;
public class JavaApplication46 {

public static void main(String[] args) {


int a[]=new int[5];
System.out.println("plzz enter values");
Scanner in=new Scanner(System.in);
for (int i=0;i<=4;i++){
a[i]=in.nextInt();
if(a[i]%2==0){
System.out.println("even");
}
else{
System.out.println("odd");
}
}
}
}

2)PROGRAM
package javaapplication46;
import java.util.*;
public class JavaApplication46 {

public void sum(int a[],int b[]){


for(int i=0;i<=4;i++){
System.out.println("result after addition is "+(a[i]+b[i]));
}
}
public static void main(String[] args) {
System.out.println("plzz enter values");
int a[]=new int[5];
int b[]=new int[5];
for(int i=0;i<=4;i++){
Scanner in=new Scanner(System.in);
a[i]=in.nextInt();
b[i]=in.nextInt();
}
JavaApplication46 obj=new JavaApplication46();
obj.sum(a, b);
}
}

TOPIC : ENCAPSULATION
1)PROGRAM
package javaapplication36;
import java.util.*;
public class JavaApplication36 {
int a,b;
public void set(int x,int y){
a=x;
b=y;
}
public void show(){
System.out.println("a ="+a);
System.out.println("b ="+b);
}
public static void main(String[] args) {
int d,c;
Scanner in=new Scanner(System.in);
c=in.nextInt();
d=in.nextInt();
NewClass obj=new NewClass();
obj.set(c, d);
obj.show();
obj.sum();
}
}

2)PROGRAM
package javaapplication27;
import java.util.*;
public class JavaApplication27 {
protected int a;
public void set(int x){
a=x;
}
public void GRADES(){
if(a>=90&&a<=100){
System.out.println("A GRADE");
}
else if(a>=80&&a<90){
System.out.println("B GRADE");
}
else if(a>=70&&a<80){
System.out.println("C GRADE");
}
else if(a>=60&&a<70){
System.out.println("D GRADE");
}
else if(a>=50&&a<60){
System.out.println("E GRADE");
}
else{
System.out.println("FAIL");
}
}
public static void main(String args[]){
int a;
Scanner in=new Scanner(System.in);
System.out.println("please enter the MARKS");
a=in.nextInt();
JavaApplication27 obj=new JavaApplication27();
obj.set( a);
obj.GRADES();
}
}
3)PROGRAM
package javaapplication27;
import java.util.*;
public class JavaApplication27 {
protected int a,b;
public void set(int x,int y){
a=x;
b=y;
}
public void mul(){
System.out.println("a*b="+(a*b));
}

public static void main(String args[]){


int a;
int b;
Scanner in=new Scanner(System.in);
System.out.println("please enter the values of x and y");
a=in.nextInt();
b=in.nextInt();
JavaApplication27 obj=new JavaApplication27();
obj.set( a,b);
obj.mul();
}
}
4)PROGRAM
package javaapplication27;
import java.util.*;
public class JavaApplication27 {
protected int a,b;
public void set(int x){
a=x;

}
public void SHOW(){
if(a%2==0){
System.out.println("even");
}
else{
System.out.println("odd");
}
}

public static void main(String args[]){


int a;
int b;
Scanner in=new Scanner(System.in);
System.out.println("please enter the values of x");
a=in.nextInt();

JavaApplication27 obj=new JavaApplication27();


obj.set( a);
obj.SHOW();
}
}

TOPIC : INHERITANCE
1)PROGRAM
package javaapplication28;
import java.util.*;
public class JavaApplication28 {
protected int a;
public void setter(int z){
a=z;
}
public void pos(){
if(a>=0){
System.out.println("positive");
}
}
public void even(){
if(a%2==0){
System.out.println("even");
}
}
}

package javaapplication28;
import java.util.*;
public class NewClass extends JavaApplication28{
public void show(){
if(a<0){
System.out.println("NEGATIVE");
}
}

public static void main(String[] args) {


int c;
Scanner in=new Scanner(System.in);
c=in.nextInt();
JavaApplication28 object=new JavaApplication28();
NewClass obj=new NewClass();
object.setter(c);
object.even();
object.pos();
obj.show();
}
}

2)program
package javaapplication36;
import java.util.*;
public class JavaApplication36 {
int a,b;
public void set(int x,int y){
a=x;
b=y;
}
public void show(){
System.out.println("a ="+a);
System.out.println("b ="+b);
}
public static void main(String[] args) {
int d,c;
Scanner in=new Scanner(System.in);
c=in.nextInt();
d=in.nextInt();
NewClass obj=new NewClass();
obj.set(c, d);
obj.show();
obj.sum();
}
}

package javaapplication36;

public class NewClass extends JavaApplication36{


public void sum(){
System.out.println("a+b = "+(a+b));
}

}
TOPIC : POLYMORPHESIM
1)PROGRAM
package javaapplication49;
public class JavaApplication49 {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
JavaApplication49 obj = new JavaApplication49();
System.out.println("Addition of two integers: " + obj.add(10, 20));
System.out.println("Addition of three integers: " + obj.add(10, 20, 30));
System.out.println("Addition of two doubles: " + obj.add(5.5, 4.5));
}
}

2)program
package javaapplication49;
public class JavaApplication49 {
public double calculateArea(double length, double width) {
return length * width;
}
public double calculateArea(double radius) {
return Math.PI * radius * radius;
}
public double calculateArea(int side) {
return side * side;
}
public static void main(String[] args) {
JavaApplication49 obj = new JavaApplication49();
System.out.println("Area of rectangle: " + obj.calculateArea(5.0, 3.0));
System.out.println("Area of circle: " + obj.calculateArea(4.0));
System.out.println("Area of square: " + obj.calculateArea(6));
}
}

3)program
package javaapplication50;

public class JavaApplication50 {

public void area() {


System.out.println("Calculating area of the shape");
}
}
class Circle extends JavaApplication50 {
double radius;
public Circle(double radius) {
this.radius =radius ;
}
@Override
public void area() {
double result = Math.PI * radius * radius;
System.out.println("Area of Circle: " + result);
}
}
class Rectangle extends JavaApplication50{
double length, width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public void area() {
double result = length * width;
System.out.println("Area of Rectangle: " + result);
}

public static void main(String[] args) {


JavaApplication50 shape1 = new Circle(5);
JavaApplication50 shape2 = new Rectangle(4, 6);
shape1.area();
shape2.area();

}
TOPIC : GRAPHICAL USER INTERFERENCE
package javaapplication50;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

// Abstract base class


abstract class Shape {
public abstract double calculateArea();
}

// Subclass for Circle


class Circle extends Shape {
private double radius;

public Circle(double radius) {


this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}

// Subclass for Rectangle


class Rectangle extends Shape {
private double length;
private double width;

public Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
public double calculateArea() {
return length * width;
}
}

// GUI Frontend
public class ShapeCalculator extends JFrame {
private CardLayout cardLayout;
private JPanel mainPanel;

public ShapeCalculator() {
setTitle("Shape Area Calculator");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

// CardLayout for switching between Circle and Rectangle inputs


cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
mainPanel.add(createCirclePanel(), "Circle");
mainPanel.add(createRectanglePanel(), "Rectangle");
JComboBox<String> shapeSelector = new JComboBox<>(new String[]{"Circle",
"Rectangle"});
shapeSelector.addActionListener(e -> {
String selectedShape = (String) shapeSelector.getSelectedItem();
cardLayout.show(mainPanel, selectedShape);
});
setLayout(new BorderLayout());
add(shapeSelector, BorderLayout.NORTH);
add(mainPanel, BorderLayout.CENTER);
}

// Circle input panel


private JPanel createCirclePanel() {
JPanel panel = new JPanel(new GridLayout(3, 2));
JLabel radiusLabel = new JLabel("Radius:");
JTextField radiusField = new JTextField();
JButton calculateButton = new JButton("Calculate Area");
JLabel resultLabel = new JLabel("Area: ");

calculateButton.addActionListener(e -> {
try {
double radius = Double.parseDouble(radiusField.getText());
Circle circle = new Circle(radius);
resultLabel.setText("Area: " + circle.calculateArea());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter a valid number!", "Error",
JOptionPane.ERROR_MESSAGE);
}
});

panel.add(radiusLabel);
panel.add(radiusField);
panel.add(new JLabel()); // Empty cell
panel.add(calculateButton);
panel.add(resultLabel);

return panel;
}

// Rectangle input panel


private JPanel createRectanglePanel() {
JPanel panel = new JPanel(new GridLayout(4, 2));
JLabel lengthLabel = new JLabel("Length:");
JTextField lengthField = new JTextField();
JLabel widthLabel = new JLabel("Width:");
JTextField widthField = new JTextField();
JButton calculateButton = new JButton("Calculate Area");
JLabel resultLabel = new JLabel("Area: ");

calculateButton.addActionListener(e -> {
try {
double length = Double.parseDouble(lengthField.getText());
double width = Double.parseDouble(widthField.getText());
Rectangle rectangle = new Rectangle(length, width);
resultLabel.setText("Area: " + rectangle.calculateArea());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter valid numbers!", "Error",
JOptionPane.ERROR_MESSAGE);
}
});

panel.add(lengthLabel);
panel.add(lengthField);
panel.add(widthLabel);
panel.add(widthField);
panel.add(new JLabel()); // Empty cell
panel.add(calculateButton);
panel.add(resultLabel);

return panel;
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
ShapeCalculator app = new ShapeCalculator();
app.setVisible(true);
});
}
}

2)PROGRAM

package javaapplication51;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
abstract class JavaApplication51 {
public abstract double calculateArea();
}
class Circle extends JavaApplication51{
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends JavaApplication51 {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
}
public class SimpleShapeCalculator extends JFrame {
private JTextField input1, input2;
private JLabel resultLabel;
public SimpleShapeCalculator() {
setTitle("Simple Shape Calculator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(5, 2, 5, 5));
JLabel shapeLabel = new JLabel("Select Shape:");
String[] shapes = {"Circle", "Rectangle"};
JComboBox<String> shapeSelector = new JComboBox<>(shapes);
JLabel input1Label = new JLabel("Input 1 (Radius/Length):");
input1 = new JTextField();
JLabel input2Label = new JLabel("Input 2 (Width, if Rectangle):");
input2 = new JTextField();
input2.setEnabled(false); // Initially disabled for "Circle"
resultLabel = new JLabel("Area: ");
JButton calculateButton = new JButton("Calculate");
shapeSelector.addActionListener(e -> {
if (shapeSelector.getSelectedItem().equals("Circle")) {
input2.setEnabled(false);
input2.setText(""); // Clear width field for Circle
} else {
input2.setEnabled(true);
}
});

calculateButton.addActionListener(e -> calculateArea((String)


shapeSelector.getSelectedItem()));
add(shapeLabel);
add(shapeSelector);
add(input1Label);
add(input1);
add(input2Label);
add(input2);
add(new JLabel()); // Empty cell for alignment
add(calculateButton);
add(new JLabel()); // Empty cell for alignment
add(resultLabel);
}

private void calculateArea(String shapeType) {


try {
double value1 = Double.parseDouble(input1.getText());
if (shapeType.equals("Circle")) {
Circle circle = new Circle(value1);
resultLabel.setText("Area: " + circle.calculateArea());
} else {
double value2 = Double.parseDouble(input2.getText());
Rectangle rectangle = new Rectangle(value1, value2);
resultLabel.setText("Area: " + rectangle.calculateArea());
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Please enter valid numbers!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
SimpleShapeCalculator app = new SimpleShapeCalculator();
app.setVisible(true);
});
}
}

THE END

You might also like