0% found this document useful (0 votes)
36 views12 pages

Advanced Java Programming Lab Exercises

The document outlines the syllabus for an Advanced Java Programming course, detailing objectives and lab exercises for students. It includes multiple Java programming tasks such as finding the highest number among three inputs, calculating grades based on marks, and implementing classes for customer accounts and employee salary structures. The document emphasizes fundamental Java concepts like classes, inheritance, polymorphism, and user input handling.

Uploaded by

shantanujoshi445
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)
36 views12 pages

Advanced Java Programming Lab Exercises

The document outlines the syllabus for an Advanced Java Programming course, detailing objectives and lab exercises for students. It includes multiple Java programming tasks such as finding the highest number among three inputs, calculating grades based on marks, and implementing classes for customer accounts and employee salary structures. The document emphasizes fundamental Java concepts like classes, inheritance, polymorphism, and user input handling.

Uploaded by

shantanujoshi445
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

Academic Year: 2024-25 Semester: II Class: FYMCA Course Code: MC506

Course Name: Advanced Java Programming Name: Shantanu Joshi UCID: 2024510023

Experiment No.1

Date:

Aim: Fundamentals of Java Programming in IntelliJ IDE

CO Mapping – CO 1

Objective:

• To understand declaration of Classes, and Methods with its all features such as Constructors,
Access Specifier
• To understandClasses, Instance variables, Methods, Constructors, Access
• Specifiers as basic fundamentals
• Implement Abstract Classes and Wrapper Classes for given problem statement
• Design and implement Inheritance, Polymorphism in JAVA
• Demonstrate Use of Static, final, super and this keyword
• Demonstrate creating user defined package, Access control protection,
• Defining interface, Implementing interface

Lab Exercise:

1) Find the Highest Number Among Three Numbers

Write a Java program that takes three integer inputs from the user and determines the highest
number among them using if-else statements. The program should display the highest number
as output.

Code:

import [Link];
class Main {
Scanner scanner = new Scanner([Link]);

[Link]("Enter first number: ");


int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
[Link]("Enter third number: ");
int num3 = [Link]();

int highest;
if (num1 >= num2 && num1 >= num3) {
highest = num1;
} else if (num2 >= num1 && num2 >= num3) {
highest = num2; } else { highest =
num3;
}

[Link]("The highest number is: " + highest);

[Link]();
}
}

Output:
2) Find Grade Based on Marks

Write a Java program that takes marks (integer) as input from the user and determines the
grade based on the following grading system:

Code:

import [Link];

class Main {
public static void main(String[] args)
{ Scanner scanner = new
Scanner([Link]);

[Link]("Enter your marks: ");


int marks = [Link]();

String grade;

if (marks >= 90) {


grade = "A+"; } else if
(marks >= 80)
{ grade = "A";
} else if (marks >= 70) {
grade = "B";
} else if (marks >= 60) {
grade = "C";
} else if (marks >= 50) {
grade = "D";
} else {
grade = "F";
}
[Link]("Your grade is: " + grade);

[Link]();
public class CustomerAccount { private
String accountNumber; private double
balance;

public CustomerAccount(String accountNumber,double initialBalance){


[Link] = accountNumber; if(initialBalance>= 0){
[Link] = initialBalance;
}else{
[Link]("Initial balance cannot be negative. Setting
balance to 0");
[Link] = 0;
}

}
public void deposit(double amount){
if(amount>0){ balance +=amount;

[Link]("Successfully deposited Rs." + amount);


}
else {
[Link]("Deposit amount must be positive.");
}
}
public void withdraw(double amount) {
if (amount > 0) {
if (amount <= balance) {
balance -= amount;
[Link]("Successfully withdrew Rs." + amount);
} else {
[Link]("Insufficient balance. Withdrawal failed.");
}
} else {
[Link]("Withdrawal amount must be positive.");
}
}
public void displayAccountDetails() {
[Link]("Account Number: " + accountNumber);
[Link]("Current Balance: Rs." + balance);
}
public static void main(String[] args) {
CustomerAccount account = new CustomerAccount("123456789", 1000);

[Link]();

[Link](500);
[Link]();

[Link](300);
[Link]();

[Link](1500);
}}
Outp
ut:

3) Write a program to deposit and withdraw from customer accounts with account number,
name, and initial balance taken as private variables.

Code:

[Link]();
[Link](-200);
[Link](-100);
} }

Output:
4) Write a Java program with a base class Employee having employee number and name
declared as private and salary as protected. getData() method inputs empno and name. putData()
displays the values of empno, name and salary. Develop a class named HourlyBasis with hours and
rate as private variables. calculate() method computes the product of rate and hours and assigns this
value to salary. Develop a class named MonthlyBasis with basic, hra and da as private variables.
calculate() method computes the product of basic and da and basic and hra and assigns the sum to
salary.

Code :
import [Link];

class Employee { private


int empno; private
String name; protected
double salary;

public void getData() {


Scanner scanner = new Scanner([Link]);
[Link]("Enter Employee Number: "); empno
= [Link]();
[Link](); // Consume newline
[Link]("Enter Employee Name: "); name
= [Link]();
}
public void putData() {
[Link]("Employee Number: " + empno);
[Link]("Employee Name: " + name);
[Link]("Salary: Rupees " + salary);
}
}
class HourlyBasis extends Employee {
private int hours; private
double rate;
public void getDetails()
{ [Link]();
Scanner scanner = new Scanner([Link]);
[Link]("Enter Hours Worked: "); hours
= [Link]();
[Link]("Enter Hourly Rate: ");
rate = [Link](); calculate();
} public void
calculate() { salary =
hours * rate;
}
} class MonthlyBasis extends
Employee { private double basic,
hra, da;
public void getDetails()
{ [Link]();
Scanner scanner = new Scanner([Link]);
[Link]("Enter Basic Salary: ");
basic = [Link]();
[Link]("Enter HRA: ");
hra = [Link]();
[Link]("Enter DA: "); da
= [Link]();
calculate();
} public void calculate()
{ salary = basic + hra +
da;
}
}
public class EmployeeMain {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);

[Link]("Choose Employee Type (1. HourlyBasis, 2. MonthlyBasis):


");
int choice = [Link]();

if (choice == 1) {
HourlyBasis hourly = new HourlyBasis();
[Link](); [Link]();
} else if (choice == 2) {
MonthlyBasis monthly = new MonthlyBasis();
[Link](); [Link]();
} else {
[Link]("Invalid choice!");
}
[Link]();
} }

Output:

5) Create a calculator take a two number from user then perform addition , subtraction ,
Multiplication ,division operation (use do while loop and switch case )

import [Link];

public class Operators {


public static void main(String[] args)
{ Scanner scanner = new Scanner([Link]);
[Link]("Enter the first number :");
int num1 = [Link]();
[Link]("Enter the second number :");
int num2 = [Link]();
int result =
0; int choice ;
do {
[Link](" Menu ");
[Link](" 1. Addition");
[Link](" 2. Subtraction");
[Link](" 3. Multiplication");
[Link](" 4. Division");
[Link](" 5. Exit");
[Link]("Enter your Choice :");
choice = [Link]();
switch (choice)
{ case 1:
[Link]("Addition = ");
result = num1 + num2;
[Link](result);
break; case 2:
[Link]("Subtraction = ");
result = num1 - num2;
[Link](result);
break; case 3:
[Link]("Multiplication = ");
result = num1 * num2;
[Link](result); break;
case
4:
[Link]("Division = ");
result = num1 / num2;
[Link](result); break;
case
5:
[Link]("5 : Exit");
[Link]("Existing..."); break;

}
while (choice <5);
}
}

Output:
Observation:
The execution of five Core Java programs demonstrated successful compilation and expected
outputs, showcasing fundamental concepts such as loops, conditionals, arrays, and object-oriented
principles. Each program efficiently handled user input and ensured proper data processing.
Exception handling mechanisms were implemented where necessary, preventing runtime errors and
ensuring smooth execution. The programs also followed optimized logic, maintaining efficiency in
computation and memory usage. Overall, the implementation validated the correct application of
Core Java principles in various problem-solving scenarios.

You might also like