Professional Documents
Culture Documents
Oops Pca1
Oops Pca1
Oops Pca1
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:
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:
double height,width,breadth;
}
class roomDemo{
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
Code:
class Student {
private String name;
private double[] marks;
public Student(String name, int numSubjects) {
this.name = name;
this.marks = new double[numSubjects];
}
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:
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) {
Output:
CIOEAM
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;
Customer 1
Enter customer number: 235617
Enter power consumed (in units): 298
Enter billing date (yyyy-MM-dd): 2023-02-19
Customer 2
Enter customer number: 235618
Enter power consumed (in units): 327
Enter billing date (yyyy-MM-dd): 2023-07-23
Code:
import java.util.Scanner;
class BankAccount {
private String accountHolderName;
private String accountNumber;
private char accountType;
private double balance;
System.out.println("\nUpdated details:");
accounts[i].displayDetails();
}
scanner.close();
}
}
Output:
Customer 1 details:
Account Holder's Name: David Das
Account Number: 3658920
Account Type: s
Balance: 25000.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
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
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;
Output:
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;}
Output:
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;
}
}
String convertOctal() {
double f1 = 0;
double f2;
f2 = val - f1;
if(f2 != 0.0) {
f1--;
f2 = val - f1;
}
while(newf1 > 0) {
ans1 = (char)(newf1 % 8 + 48) + ans1;
newf1 /= 8;
}
int term = 0;
Output: