ASSIGNMENT 4
Problem Statement-
You need to create a Java program that does the following:
1. Store Student Information:
o Create arrays to store student names, and grades for three subjects (Math,
Science, and English).
o The maximum number of students the system can handle is 100.
2. Add New Students:
o Allow the user to input the name and grades for the new student.
o Ensure that the data is stored in the appropriate arrays.
3. Update Student Grades:
o Allow the user to update the grades of any student for any subject.
o The user should be able to specify which student and which subject they want
to update.
4. Calculate and Display Average Grades:
o Write a method to calculate and display the average grade for each student
across all subjects.
o Display each student’s name along with their average grade.
5. Find the Top-Performing Student:
o Determine which student has the highest average grade.
o Display the name and average grade of the top-performing student.
Assignment Requirements:
1. Class Structure:
o Create a StudentPerformance class to encapsulate the logic of the system.
o This class should contain:
▪ Arrays for student names and their grades.
▪ Methods for adding students, updating grades, calculating averages,
and finding the top-performing student.
2. Array Usage:
o Use arrays to store and manage student data.
o Implement methods that manipulate arrays, such as adding elements, updating
elements, and iterating through arrays.
Source Code:
1.StudentPerformance.java
package studentmanagement;
public class StudentPerformance {
private static final int MAX_STUDENTS = 100;
private static final int SUBJECTS = 3; // Math, Science, English
private String[] studentNames = new String[MAX_STUDENTS];
private int[][] studentGrades = new int[MAX_STUDENTS][SUBJECTS];
private int studentCount = 0;
// Method to add a new student
public void addStudent(String name, int mathGrade, int scienceGrade, int englishGrade) {
if (studentCount < MAX_STUDENTS) {
studentNames[studentCount] = name;
studentGrades[studentCount][0] = mathGrade;
studentGrades[studentCount][1] = scienceGrade;
studentGrades[studentCount][2] = englishGrade;
studentCount++;
} else {
System.out.println(“Cannot add more students, maximum limit reached.”);
}
}
// Method to update a student’s grade for a specific subject
public void updateGrade(String name, String subject, int newGrade) {
int subjectIndex = getSubjectIndex(subject);
if (subjectIndex == -1) {
System.out.println(“Invalid subject.”);
return;
}
for (int i = 0; i < studentCount; i++) {
if (studentNames[i].equalsIgnoreCase(name)) {
studentGrades[i][subjectIndex] = newGrade;
System.out.println(subject + “ grade updated successfully for “ + name);
return;
}
}
System.out.println(“Student not found.”);
}
private int getSubjectIndex(String subject) {
switch (subject.toLowerCase()) {
case “math”:
return 0;
case “science”:
return 1;
case “boolean”:
return 2;
default:
return -1;
}
}
public void displayAverageGrades() {
for (int i = 0; i < studentCount; i++) {
double average = (studentGrades[i][0] + studentGrades[i][1] + studentGrades[i][2]) /
3.0;
average = Math.round(average * 100.0) / 100.0; // Rounding to two decimal places
System.out.println(“Student: “ + studentNames[i] + “, Average Grade: “ + average);
}
}
public void findTopStudent() {
if (studentCount == 0) {
System.out.println(“No students available.”);
return;
}
int topStudentIndex = 0;
double highestAverage = 0.0;
for (int i = 0; i < studentCount; i++) {
double average = (studentGrades[i][0] + studentGrades[i][1] + studentGrades[i][2]) /
3.0;
average = Math.round(average * 100.0) / 100.0; // Rounding to two decimal places
if (average > highestAverage) {
highestAverage = average;
topStudentIndex = i;
}
}
System.out.println(“Top-Performing Student: “ + studentNames[topStudentIndex] + “,
Average Grade: “ + highestAverage);
}
public int getStudentCount() {
return studentCount;
}
public String getStudentName(int index) {
return studentNames[index];
}
public int getStudentGrade(int index, int subjectIndex) {
return studentGrades[index][subjectIndex];
}
}
2.StudentPerformanceTest.java
package studentmanagement;
public class StudentPerformanceTest {
public static void main(String[] args) {
StudentPerformance sp = new StudentPerformance();
sp.addStudent(“Alice Green”, 90, 85, 90);
sp.addStudent(“Bob Brown”, 75, 80, 78);
sp.addStudent(“Charlie Black”, 95, 90, 85);
System.out.println(“Average Grades:”);
sp.displayAverageGrades();
// Update a student’s grade
System.out.println(“\nUpdating Bob Brown’s Science grade to 85…”);
sp.updateGrade(“Bob Brown”, “Science”, 85);
// Display average grades after update
System.out.println(“\nAverage Grades After Update:”);
sp.displayAverageGrades();
System.out.println(“\nFinding Top-Performing Student…”);
sp.findTopStudent();
}
}
Input/Output:
Average Grades:
Student: Alice Green, Average Grade: 88.33
Student: Bob Brown, Average Grade: 77.67
Student: Charlie Black, Average Grade: 90.00
Updating Bob Brown’s Science grade to 85…
Average Grades After Update:
Student: Alice Green, Average Grade: 88.33
Student: Bob Brown, Average Grade: 78.33
Student: Charlie Black, Average Grade: 90.00
Finding Top-Performing Student…
Top-Performing Student: Charlie Black, Average Grade: 90.00
Source Code:
1.Payment.java
package ecommerce.paymentsmanagement;
abstract class Payment {
protected String transactionId;
protected double amount;
protected String payerName;
protected boolean isSuccess;
public Payment(String transactionId, double amount, String payerName) {
this.transactionId = transactionId;
this.amount = amount;
this.payerName = payerName;
this.isSuccess = false; // Initialize as false
}
public abstract boolean processPayment();
public abstract void displayPaymentDetails();
public double calculateProcessingFee() {
return 0.02 * amount;
}
}
2.Discount.java
package ecommerce.paymentsmanagement;
interface Discount {
double applyDiscount(double amount);
}
3.Rewards.java
package ecommerce.paymentsmanagement;
interface Rewards {
void addRewardPoints(double amount);
}
4.CreditCardPayment.java Class
package ecommerce.paymentsmanagement;
class CreditCardPayment extends Payment implements Rewards {
private String cardNumber;
private String expiryDate;
public CreditCardPayment(String transactionId, double amount, String payerName, String
cardNumber, String expiryDate) {
super(transactionId, amount, payerName);
this.cardNumber = cardNumber;
this.expiryDate = expiryDate;
}
@Override
public boolean processPayment() {
this.isSuccess = true;
System.out.println(“Processing Credit Card Payment…”);
return isSuccess;
}
@Override
public void displayPaymentDetails() {
System.out.println(“Credit Card Payment Details:”);
System.out.println(“Transaction ID: “ + transactionId);
System.out.println(“Amount: ₹” + amount);
System.out.println(“Payer: “ + payerName);
System.out.println(“Card Number: “ + cardNumber);
System.out.println(“Expiry Date: “ + expiryDate);
System.out.println(“Payment Successful: “ + isSuccess);
}
@Override
public double calculateProcessingFee() {
return super.calculateProcessingFee() + (0.01 * amount); // 1% additional surcharge for
credit card
}
@Override
public void addRewardPoints(double amount) {
double rewardPoints = 0.01 * amount;
System.out.println(“Reward Points Added: “ + rewardPoints);
}
}
5.DebitCardPayment.java
package ecommerce.paymentsmanagement;
class DebitCardPayment extends Payment implements Discount {
private String cardNumber;
private String expiryDate;
public DebitCardPayment(String transactionId, double amount, String payerName, String
cardNumber, String expiryDate) {
super(transactionId, amount, payerName);
this.cardNumber = cardNumber;
this.expiryDate = expiryDate;
}
@Override
public boolean processPayment() {
this.isSuccess = true;
System.out.println(“Processing Debit Card Payment…”);
return isSuccess;
}
@Override
public void displayPaymentDetails() {
System.out.println(“Debit Card Payment Details:”);
System.out.println(“Transaction ID: “ + transactionId);
System.out.println(“Amount: ₹” + amount);
System.out.println(“Payer: “ + payerName);
System.out.println(“Card Number: “ + cardNumber);
System.out.println(“Expiry Date: “ + expiryDate);
System.out.println(“Payment Successful: “ + isSuccess);
}
@Override
public double applyDiscount(double amount) {
if (amount > 5000) {
return amount * 0.99;
}
return amount;
}
}
6.UPIPayment.java
package ecommerce.paymentsmanagement;
class UPIPayment extends Payment {
private String upiId;
public UPIPayment(String transactionId, double amount, String payerName, String upiId)
{
super(transactionId, amount, payerName);
this.upiId = upiId;
}
@Override
public boolean processPayment() {
this.isSuccess = true;
System.out.println(“Processing UPI Payment…”);
return isSuccess;
}
@Override
public void displayPaymentDetails() {
System.out.println(“UPI Payment Details:”);
System.out.println(“Transaction ID: “ + transactionId);
System.out.println(“Amount: ₹” + amount);
System.out.println(“Payer: “ + payerName);
System.out.println(“UPI ID: “ + upiId);
System.out.println(“Payment Successful: “ + isSuccess);
}
}
7.WalletPayment.java
package ecommerce.paymentsmanagement;
class WalletPayment extends Payment {
private String walletId;
public WalletPayment(String transactionId, double amount, String payerName, String
walletId) {
super(transactionId, amount, payerName);
this.walletId = walletId;
}
@Override
public boolean processPayment() {
this.isSuccess = true;
System.out.println(“Processing Wallet Payment…”);
return isSuccess;
}
@Override
public void displayPaymentDetails() {
System.out.println(“Wallet Payment Details:”);
System.out.println(“Transaction ID: “ + transactionId);
System.out.println(“Amount: ₹” + amount);
System.out.println(“Payer: “ + payerName);
System.out.println(“Wallet ID: “ + walletId);
System.out.println(“Payment Successful: “ + isSuccess);
}
@Override
public double calculateProcessingFee() {
return 0.015 * amount;
}
}
8.TestPaymentSystem.java
package ecommerce.paymentsmanagement;
public class TestPaymentSystem {
public static void main(String[] args) {
// Creating different payment method objects
CreditCardPayment creditCardPayment = new CreditCardPayment(“TXN123”, 6000,
“Alice”, “1234-5678-9876”, “12/25”);
DebitCardPayment debitCardPayment = new DebitCardPayment(“TXN124”, 5500,
“Bob”, “9876-5432-1234”, “11/24”);
UPIPayment upiPayment = new UPIPayment(“TXN125”, 2000, “Charlie”,
“charlie@upi”);
WalletPayment walletPayment = new WalletPayment(“TXN126”, 4000, “David”,
“WALLET123”);
// Processing and displaying payment details
processPayment(creditCardPayment);
processPayment(debitCardPayment);
processPayment(upiPayment);
processPayment(walletPayment);
}
public static void processPayment(Payment payment) {
payment.processPayment();
if (payment instanceof Discount) {
payment.amount = ((Discount) payment).applyDiscount(payment.amount);
}
if (payment instanceof Rewards) {
((Rewards) payment).addRewardPoints(payment.amount);
}
payment.displayPaymentDetails();
System.out.println(“Processing Fee: ₹” + payment.calculateProcessingFee());
System.out.println(“-------------------------");
}
}
Input/Output:
Processing Credit Card Payment…
Credit Card Payment Details:
Transaction ID: TXN123
Amount: ₹6000.0
Payer: Alice
Card Number: 1234-5678-9876
Expiry Date: 12/25
Payment Successful: true
Reward Points Added: 60.0
Processing Fee: ₹180.0
Processing Debit Card Payment…
Debit Card Payment Details:
Transaction ID: TXN124
Amount: ₹5445.0
Payer: Bob
Card Number: 9876-5432-1234
Expiry Date: 11/24
Payment Successful: true
Processing Fee: ₹108.9
Processing UPI Payment…
UPI Payment Details:
Transaction ID: TXN125
Amount: ₹2000.0
Payer: Charlie
UPI ID: charlie@upi
Payment Successful: true
Processing Fee: ₹40.0
Processing Wallet Payment…
Wallet Payment Details:
Transaction ID: TXN126
Amount: ₹4000.0
Payer: David
Wallet ID: WALLET123
Payment Successful: true
Processing Fee: ₹60.0
-------------------------
ASSIGNMENT 6
Problem Statement:
You are required to design a basic system for a Vehicle Rental Company using the concept of
inheritance in Java. The company rents out Cars and Bikes, and every vehicle has certain common
properties like brand, model, and registration number, but they also have specific attributes and
behaviors. For instance, a Car has the number of seats, while a Bike has a specific helmet
requirement.
You need to:
Create a Vehicle class as the parent class with common properties and methods.
Create two subclasses: Car and Bike.
o The Car class should have additional attributes such as numberOfSeats.
o The Bike class should have an additional attribute such as requiresHelmet.
2. Implement methods to display the details of the vehicle.
3. Demonstrate the concept of inheritance by creating objects of Car and Bike classes, setting
values for their properties, and calling their respective methods.
Step-by-Step Instructions:
1. Create the Parent Class: Vehicle
o Attributes:
▪ brand (String)
▪ model (String)
▪ registrationNumber (String)
o Methods:
▪ A constructor to initialize these fields.
▪ A method displayDetails() to print out the vehicle's common details.
2. Create the Child Class: Car
o Inherits from Vehicle.
o Additional Attribute:
▪ numberOfSeats (int)
o Methods:
▪ A constructor to initialize all fields (use super() for parent class fields).
▪ Override displayDetails() method to include the number of seats.
3. Create the Child Class: Bike
o Inherits from Vehicle.
o Additional Attribute:
▪ requiresHelmet (boolean)
o Methods:
▪ A constructor to initialize all fields.
▪ Override displayDetails() method to include whether a helmet is
required.
4. Main Class (VehicleRentalSystem)
o Create objects of Car and Bike.
o Call their methods to display details.
Source Code:
Vehicle.java
package vehiclerentalsystem;
class Vehicle {
protected String brand;
protected String model;
protected String registrationNumber;
public Vehicle(String brand, String model, String registrationNumber) {
this.brand = brand;
this.model = model;
this.registrationNumber = registrationNumber;
public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Registration Number: " + registrationNumber);
Car.java
package vehiclerentalsystem;
class Car extends Vehicle {
private int numberOfSeats;
public Car(String brand, String model, String registrationNumber, int numberOfSeats) {
super(brand, model, registrationNumber);
this.numberOfSeats = numberOfSeats;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Number of Seats: " + numberOfSeats);
Bike.java
package vehiclerentalsystem;
class Bike extends Vehicle {
private boolean requiresHelmet;
public Bike(String brand, String model, String registrationNumber, boolean requiresHelmet) {
super(brand, model, registrationNumber);
this.requiresHelmet = requiresHelmet;
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Requires Helmet: " + (requiresHelmet ? "Yes" : "No"));
VehicleRentalSystem.java
package vehiclerentalsystem;
public class VehicleRentalSystem {
public static void main(String[] args) {
// Create Car object
Car car = new Car("Toyota", "Camry", "XYZ1234", 5);
System.out.println("Car Details:");
car.displayDetails();
// Create Bike object
Bike bike = new Bike("Yamaha", "MT-15", "ABC5678", true);
System.out.println("\nBike Details:");
bike.displayDetails();
INPUT/OUTPUT:
Car Details:
Brand: Toyota
Model: Camry
Registration Number: XYZ1234
Number of Seats: 5
Bike Details:
Brand: Yamaha
Model: MT-15
Registration Number: ABC5678
Requires Helmet: Yes