Computer pgms
Asing 17
Bank Charges Loan Interest
import [Link];
class Loan {
int time;
double principal;
double rate;
double interest;
double amt;
void getdata() {
Scanner sc = new Scanner([Link]);
[Link]("Enter principal amount: ");
principal = [Link]();
[Link]("Enter time (years): ");
time = [Link]();
if (time <= 5) {
rate = 15;
} else if (time > 5 && time <= 10) {
rate = 12;
} else {
rate = 10;
}
}
void calculate() {
interest = (principal * rate * time) / 100;
amt = principal + interest;
}
void display() {
[Link]("Interest: " + interest);
[Link]("Total amount to pay: " + amt);
}
}
public class BankCharges {
public static void main(String[] args) {
Loan loan = new Loan();
[Link]();
[Link]();
[Link]();
}
}
electricity bill
public class ElectricityBill {
public double cal(int units) {
double charge = 0.0;
if (units <= 100) {
charge = units * 1.25;
}
else if (units <= 200) {
charge = 100 * 1.25;
int remainingUnits = units - 100;
charge += remainingUnits * 1.50;
}
else {
charge = 100 * 1.25;
charge += 100 * 1.50;
int remainingUnits = units - 200;
charge += remainingUnits * 1.80;
}
return charge;
}
public static void main(String[] args)
{
String consumerNo = "C12345";
String name = "John Doe";
int presentReading = 550;
int previousReading = 300;
int unitsConsumed = presentReading - previousReading;
ElectricityBill bill = new ElectricityBill();
double billAmount = [Link](unitsConsumed);
[Link]("Prog. 6\t\t\tWrite a program to calculate the monthly
electricity bill of a consumer according to the units consumed.");
[Link]();
[Link]("%-15s %-15s %-15s %-15s%n",
"Consumer No.", "Name", "Units consumed", "Amount");
[Link]("%-15s %-15s %-15s %-15s%n",
"XXX", "XXX", "XXX", "XXX"); // The format lines as requested
[Link]("%-15s %-15s %-15d %-15s%n",
consumerNo, name, unitsConsumed, [Link]("%.2f", billAmount));
[Link]("\n--- Bill Details ---");
[Link]("Present Reading: " + presentReading + " units");
[Link]("Previous Reading: " + previousReading + " units");
}
}
Asing 18
Volume Calculator
public class VolumeCalculator
{
public double volume(double r) {
return (4.0 / 3.0) * [Link] * r * r * r;
}
public double volume(double h, double r)
{
return [Link] * r * r * h;
}
public double volume(double l, double b, double h)
{
return l * b * h;
}
public static void main(String[] args)
{
VolumeCalculator calc = new VolumeCalculator();
double r_sphere = 3.5;
double volSphere = [Link](r_sphere);
[Link]("--- Volume Calculations ---");
[Link]("Sphere (r=%.2f): Volume = %.2f%n", r_sphere,
volSphere);
double h_cyl = 10.0;
double r_cyl = 2.0;
double volCylinder = [Link](h_cyl, r_cyl);
[Link]("Cylinder (h=%.1f, r=%.1f): Volume = %.2f%n", h_cyl,
r_cyl, volCylinder);
double l_cuboid = 5.0;
double b_cuboid = 4.0;
double h_cuboid = 3.0;
double volCuboid = [Link](l_cuboid, b_cuboid, h_cuboid);
[Link]("Cuboid (l=%.1f, b=%.1f, h=%.1f): Volume = %.2f%n",
l_cuboid, b_cuboid, h_cuboid, volCuboid);
}
}
SeriesSolver
public class SeriesSolver
{
public void Series(int x, int n)
{
double sum = 0.0;
[Link]("\n--- Series Sum: x^1 + ... + x^n ---");
[Link]("For x = %d and n = %d:%n", x, n);
for (int i = 1; i <= n; i++)
{
double term = [Link](x, i);
sum += term;
[Link]("Term %d (x^%d): %.0f%n", i, i, term);
}
[Link]("Total Sum = %.0f%n", sum);
}
public void Series(int p)
{
[Link]("\n--- Sequence: 0, 7, 26, 63, ...... to 'p' terms ---");
[Link]("For p = %d terms: %n", p);
for (int n = 1; n <= p; n++)
{
int term = (n * n * n) - 1;
[Link](term);
if (n < p) {
[Link](", ");
}
}
[Link]();
}
public static void main(String[] args)
{
SeriesSolver solver = new SeriesSolver();
[Link](2, 5);
[Link](7);
}}
Asing 19
CabService
import [Link];
public class CabService
{
String car_type;
double km;
double bill;
public CabService() {
car_type = "";
km = 0.0;
bill = 0.0;
}
public void accept() {
Scanner scanner = new Scanner([Link]);
[Link]("--- Cab Service Booking ---");
[Link]("Enter Car Type (AC/Non AC): ");
car_type = [Link]().trim();
[Link]("Enter Kilometers Travelled: ");
km = [Link]();
}
public void calculate() {
String type = car_type.toUpperCase();
if ([Link]("AC"))
{
if (km <= 5.0) {
bill = 150.0;
}
else
{
bill = 150.0 + (km - 5.0) * 12.0;
}
}
else if ([Link]("NON AC") || [Link]("NONAC")) {
if (km <= 5.0) {
bill = 120.0; // Up to 5 km: ₹120
}
else
{
bill = 120.0 + (km - 5.0) * 10.0;
}
}
Else
{
// Handle invalid car type input
bill = 0.0;
[Link]("\n[ERROR] Invalid car type entered. Bill calculation
failed.");
}
}
public void display() {
[Link]("\n--- Cab Service Bill ---");
if (bill == 0.0 && km > 0) {
[Link]("Invalid input or bill not calculated.");
return;
}
[Link]("%-25s %s%n", "Car Type:", car_type.toUpperCase());
[Link]("%-25s %.1f km%n", "Kilometres Travelled:", km);
[Link]("%-25s ₹ %.2f%n", "Total Bill:", bill);
[Link]("--------------------------");
}
public static void main(String[] args)
{
CabService myRide = new CabService();
[Link]();
[Link]();
[Link]();
}
}
Asing 20
Arrange
import [Link];
public class Arrange
{
private String str;
private int len;
private char[] ch;
public Arrange(String aWord)
{
str = aWord;
len = [Link]();
ch = [Link]();
}
public void acceptWord()
{
[Link]("Current word is: " + str);
[Link]("To change the word, please create a new object or
use a Scanner for input.");
Scanner sc = new Scanner([Link]);
[Link]("Enter a new word: ");
str = [Link]();
len = [Link]();
ch = [Link]();
}
public void sortLetters()
{
int n = len;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (ch[j] > ch[j + 1])
{
char temp = ch[j];
ch[j] = ch[j + 1];
ch[j + 1] = temp;
}
}
}
}
public void sortLetters()
{
[Link](ch);
}
public void displayArrangedLetters()
{
String sortedStr = new String(ch);
[Link]("Original Word: " + str);
[Link]("Arranged Letters (by ASCII): " + sortedStr);
}
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("--- Arrange Class Demo ---");
[Link]();
[Link]();
[Link]();
}
}