0% found this document useful (0 votes)
19 views18 pages

Programs - Classes - Objects - ReferenceVariables

The document provides a series of Java programs demonstrating the use of classes, objects, and methods. It includes examples such as a Hello World program, arithmetic operations, and various applications like student marks, bank accounts, and temperature conversion. Additionally, it covers object reference assignments and their implications in real-time scenarios.

Uploaded by

24is100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

Programs - Classes - Objects - ReferenceVariables

The document provides a series of Java programs demonstrating the use of classes, objects, and methods. It includes examples such as a Hello World program, arithmetic operations, and various applications like student marks, bank accounts, and temperature conversion. Additionally, it covers object reference assignments and their implications in real-time scenarios.

Uploaded by

24is100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programs with Class

1. Hello World Program

class Hello {
public static void main(String[] args) {
[Link]("Hello, Java!");
}
}

2. Addition of Two Numbers


class Addition {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
[Link]("Sum = " + sum);
}
}

3. Even or Odd Program


class EvenOdd {
public static void main(String[] args) {
int num = 7;

if (num % 2 == 0)
[Link]("Even Number");
else
[Link]("Odd Number");
}
}
4. Largest of Two Numbers
class Largest {
public static void main(String[] args) {
int a = 15;
int b = 20;

if (a > b)
[Link]("Largest = " + a);
else
[Link]("Largest = " + b);
}
}

Programs with Class & Single object

1. Student Marks Program


class Student {
int mark1;
int mark2;
int mark3;
}

class StudentDemo {
public static void main(String[] args) {
Student s = new Student();
int total;

s.mark1 = 80;
s.mark2 = 75;
s.mark3 = 90;

total = s.mark1 + s.mark2 + s.mark3;

[Link]("Total Marks = " + total);


}
}
2. Rectangle Area Program
class Rectangle {
double length;
double breadth;
}

class RectangleDemo {
public static void main(String[] args) {
Rectangle r = new Rectangle();
double area;

[Link] = 12;
[Link] = 5;

area = [Link] * [Link];

[Link]("Area of Rectangle = " + area);


}
}

3. Simple Interest Program


class Interest {
double principal;
double rate;
double time;
}
class InterestDemo {
public static void main(String[] args) {
Interest i = new Interest();
double si;
[Link] = 1000;
[Link] = 5;
[Link] = 2;
si = ([Link] * [Link] * [Link]) / 100;
[Link]("Simple Interest = " + si);
}
}
4. Employee Salary Program
class Employee {
String name;
double basicSalary;
double bonus;
}

class EmployeeDemo {
public static void main(String[] args) {
Employee e = new Employee();
double totalSalary;

[Link] = "Ravi";
[Link] = 25000;
[Link] = 5000;

totalSalary = [Link] + [Link];

[Link]("Employee Name = " + [Link]);


[Link]("Total Salary = " + totalSalary);
}
}

5. Bank Account Program


class BankAccount {
String accountHolder;
double balance;
double deposit;
}

class BankDemo {
public static void main(String[] args) {
BankAccount b = new BankAccount();
double newBalance;

[Link] = "Anu";
[Link] = 10000;
[Link] = 2000;

newBalance = [Link] + [Link];

[Link]("Account Holder = " +


[Link]);
[Link]("Updated Balance = " +
newBalance);
}
}

6. Temperature Conversion Program


class Temperature {
double celsius;
}

class TemperatureDemo {
public static void main(String[] args) {
Temperature t = new Temperature();
double fahrenheit;

[Link] = 30;

fahrenheit = ([Link] * 9/5) + 32;

[Link]("Temperature in Fahrenheit = " +


fahrenheit);
}
}
7. Average of Three Numbers
class Average {
int a;
int b;
int c;
}

class AverageDemo {
public static void main(String[] args) {
Average obj = new Average();
double avg;

obj.a = 10;
obj.b = 20;
obj.c = 30;

avg = (obj.a + obj.b + obj.c) / 3.0;

[Link]("Average = " + avg);


}
}

8. Average of Three Numbers


class Average {
int a;
int b;
int c;
}
class AverageDemo {
public static void main(String[] args) {
Average obj = new Average();
double avg;

obj.a = 10;
obj.b = 20;
obj.c = 30;
avg = (obj.a + obj.b + obj.c) / 3.0;

[Link]("Average = " + avg);


}
}

9. Percentage of Marks
class Marks {
int m1;
int m2;
int m3;
int m4;
int m5;
}

class MarksDemo {
public static void main(String[] args) {
Marks s = new Marks();
double percentage;
int total;

s.m1 = 80;
s.m2 = 75;
s.m3 = 90;
s.m4 = 85;
s.m5 = 70;

total = s.m1 + s.m2 + s.m3 + s.m4 + s.m5;


percentage = total / 5.0;

[Link]("Total = " + total);


[Link]("Percentage = " + percentage);
}
}
Programs with Class & Multiple Objects

1. Mobile Phone Example


class Mobile {
String brand;
double price;
}

class MobileDemo {
public static void main(String[] args) {

Mobile m1 = new Mobile();


Mobile m2 = new Mobile();

[Link] = "Samsung";
[Link] = 25000;

[Link] = "Apple";
[Link] = 80000;

[Link]("Mobile 1: " + [Link] + "


Price = " + [Link]);
[Link]("Mobile 2: " + [Link] + "
Price = " + [Link]);
}
}

2. Car Showroom Example


class Car {
String model;
double price;
}

class CarDemo {
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();

[Link] = "Honda City";


[Link] = 1200000;

[Link] = "Hyundai i20";


[Link] = 800000;

[Link]([Link] + " Price = " +


[Link]);
[Link]([Link] + " Price = " +
[Link]);
}
}

3. Hospital Patient Example


class Patient {
String name;
int age;
}

class HospitalDemo {
public static void main(String[] args) {

Patient p1 = new Patient();


Patient p2 = new Patient();

[Link] = "Ravi";
[Link] = 35;

[Link] = "Anu";
[Link] = 28;

[Link]("Patient 1: " + [Link] + " Age


= " + [Link]);
[Link]("Patient 2: " + [Link] + " Age
= " + [Link]);
}
}

4. Shopping Cart Example


class Product {
String productName;
double price;
int quantity;
}

class ShoppingDemo {
public static void main(String[] args) {

Product p1 = new Product();


Product p2 = new Product();

[Link] = "Laptop";
[Link] = 50000;
[Link] = 1;

[Link] = "Mouse";
[Link] = 500;
[Link] = 2;

double total1 = [Link] * [Link];


double total2 = [Link] * [Link];

[Link]([Link] + " Total = " +


total1);
[Link]([Link] + " Total = " +
total2);
}
}
5. ATM Account Example
class Account {
String name;
double balance;
}

class ATMDemo {
public static void main(String[] args) {

Account a1 = new Account();


Account a2 = new Account();

[Link] = "Kiran";
[Link] = 20000;

[Link] = "Meena";
[Link] = 15000;

[Link]([Link] + " Balance = " +


[Link]);
[Link]([Link] + " Balance = " +
[Link]);
}
}

6. Employee Payroll Example


class Employee {
String name;
double basicSalary;
double bonus;
}

class PayrollDemo {
public static void main(String[] args) {

Employee e1 = new Employee();


Employee e2 = new Employee();

[Link] = "Arun";
[Link] = 30000;
[Link] = 5000;

[Link] = "Divya";
[Link] = 35000;
[Link] = 6000;

double total1 = [Link] + [Link];


double total2 = [Link] + [Link];

[Link]([Link] + " Total Salary = " +


total1);
[Link]([Link] + " Total Salary = " +
total2);
}
}

Programs on Assigning Object Reference Variable


1. Bank Account Example (Real-Time)
class Account {
String name;
double balance;
}

class AccountDemo {
public static void main(String[] args) {

Account a1 = new Account();


[Link] = "Ravi";
[Link] = 10000;

Account a2 = a1; // reference copy


[Link] = 15000; // change using a2

[Link]("a1 Balance = " + [Link]);


[Link]("a2 Balance = " + [Link]);
}
}

2. Student Example
class Student {
String name;
int marks;
}

class StudentDemo {
public static void main(String[] args) {

Student s1 = new Student();


[Link] = "Anu";
[Link] = 80;

Student s2 = s1; // reference assignment

[Link] = 95; // modify through s2

[Link]("s1 Marks = " + [Link]);


[Link]("s2 Marks = " + [Link]);
}
}

3. Car Example
class Car {
String model;
int speed;
}
class CarDemo {
public static void main(String[] args) {

Car c1 = new Car();


[Link] = "Honda";
[Link] = 80;

Car c2 = c1; // same reference

[Link] = 120;

[Link]("c1 Speed = " + [Link]);


[Link]("c2 Speed = " + [Link]);
}
}

4. Hospital Patient Record


class Patient {
String name;
String disease;
}

class HospitalDemo {
public static void main(String[] args) {

Patient p1 = new Patient();


[Link] = "Ravi";
[Link] = "Fever";

Patient p2 = p1; // reference copy


[Link] = "Dengue"; // change using p2

[Link]("p1 Disease = " + [Link]);


[Link]("p2 Disease = " + [Link]);
}
}

5. Employee Office ID Update


class Employee {
String name;
int id;
}

class OfficeDemo {
public static void main(String[] args) {

Employee e1 = new Employee();


[Link] = "Anu";
[Link] = 101;

Employee e2 = e1; // same reference

[Link] = 202; // updated through e2

[Link]("e1 ID = " + [Link]);


[Link]("e2 ID = " + [Link]);
}
}
6. Online Shopping Order
class Order {
String product;
int quantity;
}

class ShoppingDemo {
public static void main(String[] args) {

Order o1 = new Order();


[Link] = "Laptop";
[Link] = 1;

Order o2 = o1;

[Link] = 3; // change via o2

[Link]("o1 Quantity = " + [Link]);


[Link]("o2 Quantity = " + [Link]);
}
}

7. School Classroom Example


class Classroom {
String section;
int students;
}

class SchoolDemo {
public static void main(String[] args) {

Classroom c1 = new Classroom();


[Link] = "A";
[Link] = 40;

Classroom c2 = c1;
[Link] = 45;

[Link]("c1 Students = " + [Link]);


[Link]("c2 Students = " + [Link]);
}
}

8. Demonstrating null Assignment


class Box {
double width;
}

class BoxDemo {
public static void main(String[] args) {

Box b1 = new Box();


[Link] = 10;

Box b2 = b1;

b1 = null; // b1 disconnected

[Link]("b2 width = " + [Link]);


}
}
9. ATM Account Example with Null
class Account {
double balance;
}

class ATMDemo {
public static void main(String[] args) {

Account a1 = new Account();


[Link] = 5000;

Account a2 = a1;

a1 = null; // disconnect a1

[Link]("a2 Balance = " + [Link]);


}
}

You might also like