Oops Pca1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

NAME : Goutam Hati

SUBJECT : Object Oriented Programming Lab(PCC-CS593)


ROLL NO. : 16500122066
REGISTRATION NO. : 221650120184
SEMESTER : 5th
1. Print first n prime numbers, n is taken from user.

Code:

import java.util.Scanner;

class listOfPrime {
public static void main(String[] args) {
int n,i,p=2,j;
boolean s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of terms : ");
n=sc.nextInt();
for(i=1;i<=n;)
{
s=true;
for(j=2;j<=p/2;j++)
{
if(p%j==0)
{
s=false;
break;
}
}
if(s)
{
System.out.println(" "+p);
i++;
}
//s=true;
p++;
}

}
}

Output:

Enter the number of terms : 5


2
3
5
7
11

2.Create a class "Room" which will hold the height, width and breadth of the room in three
variables.
Create another class "Roomdemo" which will use the earlier class, create instances of rooms,
set the
values of the variables and would calculate the volume of room.
Code:

public class Room {

double height,width,breadth;

}
class roomDemo{

public static void main(String[] args) {

double vol;
Room sc =new Room();
sc.height=10;
sc.width=20;
sc.breadth=30;
vol =sc.height * sc.width * sc.breadth;
System.out.println("vol="+vol);
}

Output:

vol=6000.0

3.Write a program to implement a class "student" with the following members.


i) Name of the student, ii) Marks of the student, iii )Function to assign initial values, iv)
Function to compute total average, v) Function to display the data
Write an appropriate main() to demonstrate the functioning of the above.

Code:

class Student {
private String name;
private double[] marks;
public Student(String name, int numSubjects) {
this.name = name;
this.marks = new double[numSubjects];
}

public void setMarks(double[] marks) {


if (marks.length == this.marks.length) {
for (int i = 0; i < marks.length; i++) {
this.marks[i] = marks[i];
}
}
else {
System.out.println("Number of subjects does not match the marks array length.");
}
}

public double calculateTotal() {


double total = 0.0;
for (double mark : marks) {
total += mark;
}
return total;
}

public double calculateAverage() {


return calculateTotal() / marks.length;
}

public void displayData() {


System.out.println("Student Name: " + name);
System.out.println("Marks in each subject:");
for (int i = 0; i < marks.length; i++) {
System.out.println("Subject " + (i + 1) + ": " + marks[i]);
}
System.out.println("Total Marks: " + calculateTotal());
System.out.println("Average Marks: " + calculateAverage());
}
}

class StudentDemo {
public static void main(String[] args) {
Student student1 = new Student("John", 5);
double[] marks = {85.5, 90.0, 78.5, 92.0, 88.5};
student1.setMarks(marks);
student1.displayData();
}
}

Output:

Student Name: John


Marks in each subject:
Subject 1: 85.5
Subject 2: 90.0
Subject 3: 78.5
Subject 4: 92.0
Subject 5: 88.5
Total Marks: 434.5
Average Marks: 86.9

4. Write a class to print abbreviation of a name from user.

Code:

import java.util.*;
class lab2 {
static void printAbbreviation(String name) {
String ans = "";
if(name.charAt(0) != ' ') {
ans += name.charAt(0);
}

int n = name.length();
for(int i=1;i<n;i++) {
if(name.charAt(i) == ' ') {
if(i != n-1 && name.charAt(i+1) != ' ') {
ans += name.charAt(i+1);
i++;
}
}
}

System.out.println(ans.toUpperCase());
}
public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


String name = sc.nextLine();
printAbbreviation(name);
sc.close();
}
}

Output:

Calcutta Institute Of Engineering and Management

CIOEAM

5. An electric power distribution company charges its domestic consumers as follows:

Consumption Units Rate of charge


0-200 Rs. 0.50 per unit
201 -400 Rs.0.65 per unit excess of 200
401 - 600 Rs.0.80 per unit excess of 400
601 and above Rs.l .00 per unit excess of 600

Write a class "ElectricityBill", which will read the customer number and power consumed
and billing
date and prints the amount to be paid by the customer and bill payment date (within 10 days
from the date of bill generation).
Code:

import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ElectricityBillCalculator {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of customers: ");


int numCustomers = scanner.nextInt();
scanner.nextLine(); // Consume the newline character

for (int i = 1; i <= numCustomers; i++) {


System.out.println("\nCustomer " + i);

System.out.print("Enter customer number: ");


String customerNumber = scanner.nextLine();

System.out.print("Enter power consumed (in units): ");


double powerConsumed = scanner.nextDouble();

System.out.print("Enter billing date (yyyy-MM-dd): ");


scanner.nextLine(); // Consume the newline character
String billingDateStr = scanner.nextLine();

System.out.println("\nGenerating bill for Customer " + i + "...\n");

double unitCost = 0.5; // Cost per unit in rupees


double amountToPay = powerConsumed * unitCost;

System.out.println("Customer Number: " + customerNumber);


System.out.println("Power Consumed: " + powerConsumed + " units");
System.out.println("Billing Date: " + billingDateStr);
System.out.println("Amount to Pay: " + amountToPay + " rupees");

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");


try {
Date billingDate = dateFormat.parse(billingDateStr);
Date dueDate = new Date(billingDate.getTime() + TimeUnit.DAYS.toMillis(10));
System.out.println("Bill Payment Date (within 10 days): " +
dateFormat.format(dueDate));
} catch (Exception e) {
System.out.println("Error parsing date: " + e.getMessage());
}
}
scanner.close();
}
}
Output:

Enter the number of customers: 2

Customer 1
Enter customer number: 235617
Enter power consumed (in units): 298
Enter billing date (yyyy-MM-dd): 2023-02-19

Generating bill for Customer 1...

Customer Number: 235617


Power Consumed: 298.0 units
Billing Date: 2023-02-19
Amount to Pay: 149.0 rupees
Bill Payment Date (within 10 days): 2023-03-01

Customer 2
Enter customer number: 235618
Enter power consumed (in units): 327
Enter billing date (yyyy-MM-dd): 2023-07-23

Generating bill for Customer 2...

Customer Number: 235618


Power Consumed: 327.0 units
Billing Date: 2023-07-23
Amount to Pay: 163.5 rupees
Bill Payment Date (within 10 days): 2023-08-02

6. Declare a class "BankAccount" with the following:


i. Account Holder's Name
ii. Account Number
Account Type (S for savings and C for Current)
Balance amount
Methods:
i. Initialize data
ii. Deposit money
Withdrawal of money — minimum deposit Rs. 1 ,000/-
iii. To display all items
iv. Write an appropriate main() to maintain details and access methods for N customers.

Code:

import java.util.Scanner;
class BankAccount {
private String accountHolderName;
private String accountNumber;
private char accountType;
private double balance;

public BankAccount(String accountHolderName, String accountNumber, char accountType,


double balance) {
this.accountHolderName = accountHolderName;
this.accountNumber = accountNumber;
this.accountType = accountType;
this.balance = balance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
System.out.println("Deposited: " + amount);
} else {
System.out.println("Invalid deposit amount.");
}
}

public void withdraw(double amount) {


if (amount > 0 && (balance - amount) >= 1000) {
balance -= amount;
System.out.println("Withdrawn: " + amount);
} else {
System.out.println("Insufficient funds or invalid withdrawal amount.");
}
}

public void displayDetails() {


System.out.println("Account Holder's Name: " + accountHolderName);
System.out.println("Account Number: " + accountNumber);
System.out.println("Account Type: " + accountType);
System.out.println("Balance: " + balance);
}
}
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of customers: ");


int n = scanner.nextInt();
scanner.nextLine();

BankAccount[] accounts = new BankAccount[n];

for (int i = 0; i < n; i++) {


System.out.println("Customer " + (i + 1) + " details:");
System.out.println("Enter Account Holder's Name: ");
String accountHolderName = scanner.nextLine();
System.out.println("Enter Account Number: ");
String accountNumber = scanner.nextLine();
System.out.println("Enter Account Type (s for savings, c for current): ");
char accountType = scanner.nextLine().charAt(0);
System.out.println("Enter Initial Balance: ");
double initialBalance = scanner.nextDouble();
scanner.nextLine();

accounts[i] = new BankAccount(accountHolderName, accountNumber, accountType,


initialBalance);
}

for (int i = 0; i < n; i++) {


System.out.println("\nCustomer " + (i + 1) + " details:");
accounts[i].displayDetails();

System.out.println("\nEnter amount to deposit: ");


double depositAmount = scanner.nextDouble();
accounts[i].deposit(depositAmount);

System.out.println("Enter amount to withdraw: ");


double withdrawAmount = scanner.nextDouble();
accounts[i].withdraw(withdrawAmount);

System.out.println("\nUpdated details:");
accounts[i].displayDetails();
}
scanner.close();
}
}

Output:

Enter the number of customers:


3
Customer 1 details:
Enter Account Holder's Name:
David Das
Enter Account Number:
3658920
Enter Account Type (s for savings, c for current):
s
Enter Initial Balance:
25000
Customer 2 details:
Enter Account Holder's Name:
Joy Sannyal
Enter Account Number:
3652980
Enter Account Type (s for savings, c for current):
c
Enter Initial Balance:
39000
Customer 3 details:
Enter Account Holder's Name:
Niraj Khan
Enter Account Number:
3659820
Enter Account Type (s for savings, c for current):
s
Enter Initial Balance:
20000

Customer 1 details:
Account Holder's Name: David Das
Account Number: 3658920
Account Type: s
Balance: 25000.0

Enter amount to deposit:


200
Deposited: 200.0
Enter amount to withdraw:
20000
Withdrawn: 20000.0

Updated details:
Account Holder's Name: David Das
Account Number: 3658920
Account Type: s
Balance: 5200.0

Customer 2 details:
Account Holder's Name: Joy Sannyal
Account Number: 3652980
Account Type: c
Balance: 39000.0

Enter amount to deposit:


0
Invalid deposit amount.
Enter amount to withdraw:
20000
Withdrawn: 20000.0

Updated details:
Account Holder's Name: Joy Sannyal
Account Number: 3652980
Account Type: c
Balance: 19000.0

Customer 3 details:
Account Holder's Name: Niraj Khan
Account Number: 3659820
Account Type: s
Balance: 20000.0

Enter amount to deposit:


3200
Deposited: 3200.0
Enter amount to withdraw:
2580
Withdrawn: 2580.0

Updated details:
Account Holder's Name: Niraj Khan
Account Number: 3659820
Account Type: s
Balance: 20620.0

7. Write a program to find given substring in a line of text taken as input and replace with
another substring.

Code :

import java.util.Scanner;

public class SubstringReplacement {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a line of text: ");
String inputText = scanner.nextLine();
System.out.print("Enter the substring to find: ");
String findSubstring = scanner.nextLine();
System.out.print("Enter the replacement substring: ");
String replaceSubstring = scanner.nextLine();
String result = inputText.replace(findSubstring, replaceSubstring);
System.out.println("Modified text: " + result);
scanner.close();
}
}

Output:

Enter a line of text: I am a student of CIEM


Enter the substring to find: student
Enter the replacement substring: Trainee
Modified text: I am a Trainee of CIEM
8. Write a program to implement a class "Salesman" with the attributes: name, salesman
code,sales amount and commission. The company calculates the commission of salesman
according to the following formula
8% if sales<2000
10% if sales<=2000 and >=5000
12% If sales>5000
Create 5 salesman objects and find their commissions for their sales.

Code:

class Salesman {
private String name;
private int salesmanCode;
private double salesAmount;
private double commission;
public Salesman(String name, int salesmanCode, double salesAmount) {
this.name = name;
this.salesmanCode = salesmanCode;
this.salesAmount = salesAmount;}

public void calculateCommission() {


if (salesAmount < 2000) {
commission = salesAmount * 0.08; // 8% commission
} else if (salesAmount >= 2000 && salesAmount <= 5000) {
commission = salesAmount * 0.10; // 10% commission
} else {
commission = salesAmount * 0.12; // 12% commission
}
}

public void displaySalesmanInfo() {


System.out.println("Salesman Name: " + name);
System.out.println("Salesman Code: " + salesmanCode);
System.out.println("Sales Amount: " + salesAmount);
System.out.println("Commission: " + commission);
System.out.println("--------------------------");
}
}

public class SalesmanDemo {


public static void main(String[] args) {
// Create 5 Salesman objects with their information
Salesman salesman1 = new Salesman("John", 101, 1500);
Salesman salesman2 = new Salesman("Alice", 102, 2200);
Salesman salesman3 = new Salesman("Bob", 103, 5500);
Salesman salesman4 = new Salesman("Eve", 104, 3500);
Salesman salesman5 = new Salesman("Mike", 105, 1800);

// Calculate commissions for each salesman


salesman1.calculateCommission();
salesman2.calculateCommission();
salesman3.calculateCommission();
salesman4.calculateCommission();
salesman5.calculateCommission();
// Display information for each salesman
salesman1.displaySalesmanInfo();
salesman2.displaySalesmanInfo();
salesman3.displaySalesmanInfo();
salesman4.displaySalesmanInfo();
salesman5.displaySalesmanInfo();
}
}

Output:

Salesman Name: John


Salesman Code: 101
Sales Amount: 1500.0
Commission: 120.0
--------------------------
Salesman Name: Alice
Salesman Code: 102
Sales Amount: 2200.0
Commission: 220.0
--------------------------
Salesman Name: Bob
Salesman Code: 103
Sales Amount: 5500.0
Commission: 660.0
--------------------------
Salesman Name: Eve
Salesman Code: 104
Sales Amount: 3500.0
Commission: 350.0
--------------------------
Salesman Name: Mike
Salesman Code: 105
Sales Amount: 1800.0
Commission: 144.0
--------------------------

9. Write a java class ‘Number’ which will hold a decimal number. Create another class ‘Octal’
Which will extend the class number and method namely ‘ConvertOctal’ it will create the
decimal no. into equivalent octal . Write a main class to demonstrate the functioning of the
above.

Code:

import java.util.*;
class Number {
double val;

Number(double n) {
val = n;
}
}

class Octal extends Number {


Octal(double val) {
super(val);
}

String convertOctal() {
double f1 = 0;
double f2;

while(f1 < val)f1++;

f2 = val - f1;
if(f2 != 0.0) {
f1--;
f2 = val - f1;
}

int newf1 = (int)f1;

String ans1 = "", ans2 = "";

while(newf1 > 0) {
ans1 = (char)(newf1 % 8 + 48) + ans1;
newf1 /= 8;
}

int term = 0;

while(f2 > 0.0) {


if(term > 6)break;
f2 *= 8;
int tem = (int)f2;
f2 -= tem;
term++;
ans2 = ans2 + (char)(tem + 48);
}

return ans1 + '.' + ans2;


}
}
class p1 {
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Decimal Number: ");
double val = sc.nextDouble();
Octal ob = new Octal(val);
String ans = ob.convertOctal();
System.out.println("The a Octal Value of the given Number is: " + ans);
}
}

Output:

Enter a Decimal Number: 25.5


The a Octal Value of the given Number is: 31.4

You might also like