ANS Object Oriented Programming Through JAVA - B TEC - 250411 - 091402
The document contains various Java programming examples and exercises related to object-oriented programming concepts such as classes, inheritance, interfaces, and exception handling. It includes code snippets for calculating attendance, electricity bills, and creating user forms, along with explanations of methods and functionalities. The document serves as a study guide for students in computer engineering and computer science programs, focusing on practical applications of Java programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views8 pages
ANS Object Oriented Programming Through JAVA - B TEC - 250411 - 091402
The document contains various Java programming examples and exercises related to object-oriented programming concepts such as classes, inheritance, interfaces, and exception handling. It includes code snippets for calculating attendance, electricity bills, and creating user forms, along with explanations of methods and functionalities. The document serves as a study guide for students in computer engineering and computer science programs, focusing on practical applications of Java programming.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
Progranys: B. Tech, MBA Tech and BTL Year: IVIIVIV Semester: 1V/V/VIIL
Stream/s : Computer Engineering, Computer Science and computer
Subject: Object Oriented Programming Through JAVA,
Synaptic- Set A
Final Examination/Re-Examination ( Re-exam year)
La Class with method and variable 2M
Enough méthod 2M
Result printing 1M
import java.util.*;
public class Main {
public static void main(Stringl] args) {
// Example usage:
int goal;
Scanner sc-new Scanner(System.in);
goal=sc.nextint();
System.out printin( “Smallest positive integer n for which sum >= gos
enough(goal));
}
public static int enough(int goal) {
int sum = 0;
int
while (true) {
sum += n;
if (sum >= goal) {
return n;
}
nes; |
}
} |
}
b Variable declaration 1M.
Input() 2M
Compute() 2M
\port java.util.Scanner;
public class Eligible {
private int classesHeld;
private int classesAttended;
J/ Method to accept data from the user
public void input() {
Scanner scanner = new Scanner(System.in);
System.out.printin("Enter the number of classes held: ");
this.classesHeld = scanner.nextint();
System.out printin("Enter the number of classes attended: ");
this.classesAttended = scanner.nextint();
scanner.close(};
}
-———_—
{/ Method to compute percentage and verify eligibilitypublic void compute() {
double attendancePercentage = (double) classesAttended / classesHeld * 100;
system.out.printin( "Attendance Percentage: " + attendancePercentage + "%");
if attendancePercentage >= 80) {
‘System.out.printin("Student permitted to appear in the exam");
yelse {
‘ystem.out.printin( "Student not allowed to appear in the exam");
}
J
public static void main(String[} args) {
Eligible student = new Eligible();
student.input();
student.compute();
,
i
Le
Only form window created = 1M
Buttons added onthe form = 2M.
Buttons functional 2M
import javax.swing.*;
import java.awt.event.*;
public class UserForm extends JFrame implements ActionListener {
private JButton submitButton;
private JButton cancelButton;
public UserForm() {
setTitle("User Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel= new JPanel();
submitButton = new JButton("SUBMIT");
canceiButton = new JButton("CANCEL"
submitButton.addActionListener(thi
cancelButton.addActiontistener(this);
panel.add(submitButton);
panel.add{cancelButton);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if e.getSource() == submitButton) (
System.out.printin( "Submitted"else if (e-getSource()
¥
}
public static void main(String{] args) {
ew UserForm();,
-ancelButton) (
System.out.printin("Cancelled");
class Electricity {
protected int units;
J Constructor
public Electricity(int units) {
this.units = units;
‘
// Function to compute the cost
public double bill) {
double cost
if (units <= 100) {
cost
yelse {
nits * 0.5(
cost = 100 * 0.50 + (units - 100) * 0.60;
}
return cost;
d
1
class More_Electricity extends Electricity {
J/ Constructor
}
1d ArrayIndexOutOfBoundsException SM
public class JavaExceptionExample{
Public static void main(String args{]){
ty
Iicode that may raise exception
int af]-new int(5];
a[10}=50;
}
catch(ArrayindexOutOfBoundsException e)
{
‘System.out.printin(e);
}
)
= 1
% Class electricity
Bill method 2M
Constructor 1M
Class more_electricity Inheritance 1M
override Bill method 2M
constructor 2M
calling methods in main() 2Mpublic More_Electricity(int units) {
super(units);
t
// Overridden function to add surcharge
@override
public double bill) {
double totalCost = super-bill(}
if (totalCost > 250.00) {
double difference = totalCost - 250.00;
totalCost += difference * 0.15;
;
return totalCost;
.
public static void main(String(] args) {
| // xample usage
Electricity customer = new Electricity(80);
More_Electricity customer2 = new More_Electricity(350);
System.out.printIn(*Bill for customer 1: Rs." + customer1.bill));
System.out.printin("Bill for customer 2: Rs." + customer2.bill());
}
x
Class members declaration 2M
Class employee inheritance 3M
Class manager inheritance 3M.
Creating objects and Calling methods 2M
class Member {
private String name;
private int age;
private String phoneNumber;
private String address;
private double salary;
public Member(String name, int age, String phoneNumber, String
address, double salary) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
}
public void printSalary() {
System.out.printin("Salary "+ salary);
}
1
class Employee extends Member {
private String specialization;
public Employee(String name, int age, String phoneNumber,
String address, double salary, String
specialization) {
super(name, age, phoneNumber, address, salary);this specialization = specialization;
I
}
class Manager extends Member{
private String department;
public Manager(String name, int age, String phoneNumber,
String address, double salary, String
department) {
super(name, age, phoneNumber, address, salary);
this.department = department;
t
}
public class Main {
Public static void main(String{] args) {
Employee employee = new Employee("Ram", 35, "+919826585882",
"ShirpurHiome", 25000, "CSE");
Manager manager = new Manager("Shyam", 40, "919826585889",
"Dhule", 35000, "AIML");
employee.printSalary();
‘manager.printSalary();
}
}
Interfaces and class creation 3M,
Inheritance of three classes 3M
Abstract methods definition in relevant classes 3M
Object creations 1M
// \nterface representing land animals
interface LandAnimal (
void move();
}
// Interface representing aquatic animals
interface AquaticAnimal {
void swim();
d
// Parent class representing animals
class Animal {
void makeSound{) {
System.out.printin("Animal is making a sound");
}
}
// Child class representing land mammals
class LandMammal extends Animal implements LandAnimal {
@Override
public void move() {
System.out.printin("Land mammal is moving on land");
?
}// Child class representing aquatic mammals
class AquaticMiammal extends Animal implements AquaticAnimal {
@Override
public void swim() {
System.out printin("Aquatic mammal is swimming in water");
)
}
// Hybrid inheritance combining land and aquatic features
class HybridMammal extends LandMammal implements AquaticAnimal {
@Override
public void swim() {
‘System.out.println("Hybrid mammal is swimming in water");
}
?
public class Main {
public static void main(String{] args) {
// Create objects of different types of mammals
LandMammal landMammal = new LandMammall);
‘AquaticMammal aquaticMammal = new AquaticMammal();
HybridMammal hybridMammal = new HybridMammal();
1{ Call respective methods for each mammal
System.out.printin(""Land Mammal Actions:");
landMammal.makeSound();
landMammal.move();
System.out.printin("\nAquatic Mammal Action:
aquaticMammal.makeSound();
aquaticMammal.swim();
System.out printIn("\nHybrid Mammal Actions:
hybridMammal.makeSound();
hybridMammal.move(); // Inherited from LandMammal
hybridMammal.swim(); // Inherited from AquaticAnimal
Z
}
Creating arraylist_ 2M
Input the array elements 2M
Sum calculation 3M
Object creation 1M
Printing 2M
import java.util ArrayList;
import java.util. Scanner;
public class SumOfArrayListElements {
public static void main(Stringf] args) (
1 Create an ArrayList to store integers,ArrayListsinteger> list = new ArrayListe>();
1/ Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter the number of elements in the ArrayList
System.out.print("Enter the number of elements in the ArrayList: ");
int n = scanner.nextint();
// Prompt the user to enter the elements of the ArrayList
‘System.out.printin("Enter the elements of the ArrayList:");
for (int i= 0; i< 1; it+){
int num = scanner.nextint();
list.add(num);
i
// Calculate the sum of all elements in the ArrayList
int sum = 0;
for (int num : list) {
sum += num;
y
/{ Display the sum of all elements in the ArrayList
System.out.printin("Sum of ArrayList elements: "+ sum);
// Close the Scanner object
scanner.close();
Form preparation 6M
Action perform 4M
import java.awt.*;
import java.awtevent.*;
import javax.swing.*;
public class ‘Test implements ActionListener {
JFrame fl;
ILabel 10,11,12,13,14;
JTextField tfl ,tf2,tf3,tf4;
Button bl;
static int ¢;
Test(){
fl =new JFrame();
10 = new JLabel("Area and Perimeter of Rectangle");
11 =new JLabel("Enter Length — :");
12=new ILabel("Enter Width
13=new JLabel("Areais. —:"
14 = new ILabel(*Perimeter is
"):tf] = new JTextField(10);
1f2 = new JTextField(10);
tf3 = new JTextField(10);
t€4 = new JTextField(10);
bl =new JButton(" Calculate, ";
10.setFont(new Font(("Aria"),Font.BOLD,16));
bl setFént(new Font(("“Aria")Font.BOLD, 16);
fl.setLayout(new FlowLayout();
Fl.add(l0);,
flada(lt);,
fl.add(ttl);
fladd(2);
fl.addctt2);
fl.add(b1);
fl.add(13);,
fLadd(tt3);
fl.add(4),
fl.add(tf4);
fl.setVisible(true);
{1 setSize(600,400);
fl setTitle("Area_Perimeter_Test");
bl.addActionListener(this);
public void actionPerformed(ActionEvent e){
String m = tfl.getText();
String n= tf2.getText(;
float a = Float.parseFloat(m)*Float.parseFloat(n);
float b = 2*(Float parseFloat(m)Float parseFloat(n));
{B3.setText(at"");
‘tf4.setText(b+");
public static void main(String[] args) {
// TODO Auto-generated method stub
new Test();