Assignment Comp 1
Assignment Comp 1
Q6. Write a program in Java using a method Discount( ), to calculate a single discount or a
successive discount. Use overload methods Discount(int), Discount(int,int) and
Discount(int,int,int) to calculate single discount and successive discount respectively.
Calculate and display the amount to be paid by the customer after getting discounts on the
printed price of an article.
CODE
import java.util.*;
return p - (p * a / 100.0);
}
public double Discount(double p, int a, int b)
{
double p1 = p - (p * a / 100.0);
double p;
System.out.println("Enter price: ");
p=in.nextDouble();
Adit Singhal 10 Ɣ
int d1,d2,d3;
System.out.println("Enter first discount: ");
d1=in.nextInt();
Q7. Write a program to input a three digit number. Use a method int Armstrong(int n) to
accept the number. The method returns 1, if the number is Armstrong, otherwise zero(0).
Sample Input: 153
Sample Output: 153 ⇒ 13 + 53 + 33 = 153
CODE
import java.util.Scanner;
int originalNumber = n;
int sum = 0;
for (int t = n; t!= 0; t /= 10)
{
int digit = t % 10;
{
System.out.println(num + " is an Armstrong number");
Adit Singhal 10 Ɣ
} else
{
System.out.println(num + " is not an Armstrong number");
}
scanner.close();
}
}
OUTPUT
Adit Singhal 10 Ɣ
double volume(double h, double r) — with height(h) and radius(r) as the arguments, returns
the volume of a cylinder using the formula: V = (22/7) * r * r * h
double volume(double 1, double b, double h) — with length(l), breadth(b) and height(h) as
the arguments, returns the volume of a cuboid using the formula: V = lbh ⇒ (length *
breadth * height)
CODE
import java.util.*;
public class VolumeCalculator
{
public double volume(double r)
{
return (4.0 / 3.0) * (22.0 / 7.0) * r * r * r;
}
int r,rc,h,hc,l,b;
System.out.println("Enter radius of sphere ");
r=in.nextInt();
Adit Singhal 10 Ɣ
rc=in.nextInt();
System.out.println("Enter height of cylinder ");
hc=in.nextInt();
double cylinderVolume = vc.volume(rc, hc);
b=in.nextInt();
System.out.println("Enter height of cuboid ");
h=in.nextInt();
double cuboidVolume = vc.volume(l, b, h);
System.out.println("Volume of the cuboid: " + cuboidVolume);
}
}
OUTPUT
Adit Singhal 10 Ɣ
CODE
import java.util.*;
public class NumberOperations
{
void Number(int n, int d)
{
int c = 0;
int t= n;
while (t> 0)
{
if (t % 10 == d)
{
c++;
}
t /= 10;
}
System.out.println("Frequency of digit " + d + " = " + c);
}
void Number(int n1)
{
int sum = 0;
int t = n1;
while (t > 0)
{
int d = t % 10;
if (d % 2 == 0)
{
sum += d;
}
t /= 10;
Adit Singhal 10 Ɣ
}
System.out.println("Sum of even digits = " + sum);
}
public static void main()
{
NumberOperations ob = new NumberOperations();
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter a number: ");
a=in.nextInt();
System.out.println("Enter the number of which you desire to find the frequency of: ");
b=in.nextInt();
ob.Number(a, b);
System.out.println("Enter a number to find the sum of its even number digits: ");
c=in.nextInt();
ob.Number(c);
}
}
OUTPUT
Adit Singhal 10 Ɣ
Q10. Define a class named Movie Mania with the following description
Data members: int year to store the year of release of the movie
String title to store the title of the movie
float rating to store the popularity rating of the movie (minimum rating = 0.0, maximum rating
= 6.03.
Member methods:
Movie Maniacs- default constructor to initialize the data members with legal initial valves
void accept() - to input and store the year, title and rating
void display() - to display the movie title along with a message based on the rating as given.
below.
Rating Message
0-0-2.0. Flop
2-1-3.4 semi-Hit
3.5-4.5 Hit
46-50 Super hit
Write the main() method to create of the class and invoke the above member methods
accordingly.
INPUT
import java.util.*;
class MovieMania
{
int year;
String title;
float rating;
MovieMania()
{ year = 2000;
title = "Unknown";
rating = 0.0f;
}
void accept()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the year of release: ");
year = in.nextInt();
in.nextLine();
System.out.print("Enter the title of the movie: ");
title = in.nextLine();
Adit Singhal 10 Ɣ
OUTPUT
Adit Singhal 10 Ɣ
Q11. Design a java class to make a courier company charges for the courier based on the weight of
the parcel. Define a class with the following specifications.
class name: courier
Member variables:
name-name of the customer
weight-weight of the parcel in kilograms
address - address of the recipient
bill amount to be paid
type 'D' domestic, ‘I’- International
Member methods
void accept() - to accept the details using the methods of the scanner class only
void calculate () -to calculate the bill as per the following criteria
First 5 kgs RS 800
Next 5 kgs RS 700
Above 10 kgs RS 500 and additional if the type amount of of RS 1600 is charged courier is
International
void print() to print the details
void main()- to create an object of the class and invoke the methods.
INPUT
import java.util.*;
class Courier
{
String name;
double weight;
String address;
double billAmount;
char type;
void accept()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the name of the customer: ");
name = in.nextLine();
System.out.print("Enter the weight of the parcel in kilograms: ");
weight = in.nextDouble();
in.nextLine();
System.out.print("Enter the address of the recipient: ");
address = in.nextLine();
System.out.print("Enter the type of courier (D for domestic, I for international): ");
type = in.next().charAt(0);
}
void calculate()
Adit Singhal 10 Ɣ
{
if (weight <= 5)
{
billAmount = weight * 800;
} else if (weight <= 10)
{
billAmount = 5 * 800 + (weight - 5) * 700;
} else
{
billAmount = 5 * 800 + 5 * 700 + (weight - 10) * 500;
}
if (type == 'I')
{
billAmount += 1600;
}
}
void print()
{
System.out.println("\nCustomer Name: " + name);
System.out.println("Weight of the Parcel: " + weight + " kg");
System.out.println("Address of the Recipient: " + address);
System.out.println("Courier Type: " + (type == 'D' ? "Domestic" : "International"));
System.out.println("Bill Amount: Rs " + billAmount);
}
public static void main()
{
Courier courier = new Courier();
courier.accept();
courier.calculate();
courier.print();
}
}
OUTPUT
Adit Singhal 10 Ɣ
Q12. Create a class named details Pizza that stores about a pizza. It should contain the
following
Instance variables:
String pizzasize - to store the size of the pizza small, medium or large
int cheese - the number of cheese toppings
int pepperoni- the number of pepperoni toppings.
int mushroom-the number of mushroom toppings
Member methods:
constructor - to initialize all the instance variables.
calculatecost() - a public method that returns a double valve, that is, the cost of the pizza.
Pizza cost is calculated as follows
Small: RS 500 + RS 25 per topping
Medium: RS 650 + Rs 25 per topping
Large: RS 800 + RS 25 per topping
Pizza Description() - A public method that returns a string containing the pizza size, quantity
of each topping and the pizza cost as calculated by calculatecosr
INPUT
import java.util.*;
public class Pizza
{
String pizzaSize;
int cheese;
int pepperoni;
int mushroom;
public Pizza(String size, int tc, int tp, int tm)
{
pizzaSize = size;
cheese = tc;
pepperoni = tp;
mushroom = tm;
}
public double CalculateCost()
{
double totalCost = 0;
double topCost = 25 * (cheese + pepperoni + mushroom);
if (pizzaSize == "Small")
totalCost = 500 + topCost;
else if(pizzaSize == "Medium")
totalCost = 650 + topCost;
Adit Singhal 10 Ɣ
else
totalCost = 800 + topCost;
return totalCost;
}
public String PizzaDescription()
{
double cost = CalculateCost();
String desc = "Size: " + pizzaSize + " Cheese: " + cheese + " Pepperoni: " + pepperoni + "
Mushroom: " + mushroom + " Cost: Rs. " + cost;
return desc;
}
public static void main()
{
Scanner in=new Scanner(System.in);
String s;
int c,m,pep;
System.out.println("Enter size of pizza: ");
s=in.nextLine();
System.out.println("Enter number of cheese toppings: ");
c=in.nextInt();
System.out.println("Enter number of pepproni: ");
pep=in.nextInt();
System.out.println("Enter number of mushrooms: ");
m=in.nextInt();
Pizza p = new Pizza(s,c,m,pep);
String desc = p.PizzaDescription();
System.out.println(desc);
}
}
OUTPUT
Adit Singhal 10 Ɣ
Q13. Design a class to represent a bank account. Include the following members.
Data members
•Name of the depositor
Account number
Type of account
Balance Methods amount in the the account
To assign initial valves
•To deposit an amount
•To with draw balance an amount after checking
To dis play the name and balance
Do write proper constructor functions
INPUT
import java.util.*;
class BankAccount
{
private String name;
private long accno;
private char type;
private double bal;
BankAccount()
{
name = "";
accno = 0;
type = '0';
bal = 0.0;
}
BankAccount(String n, long accnumber, char t, double b)
{
name = n;
accno = accnumber;
type = t;
bal = b;
}
public void deposit(double amt)
{
bal = bal + amt;
System.out.println("Amount deposited: Rs. " + amt);
System.out.println("Balance: Rs. " + bal);
}
public void withdraw(double amt)
{
if(amt <= bal)
{
Adit Singhal 10 Ɣ
Specifications:
INPUT
import java.util.*;
public class Array
{
private int a[] = new int[10];
private int b[] = new int[10];
void getlist()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter 10 integers for the first array:");
for (int i = 0; i < 10; i++)
{
a[i] = in.nextInt();
}
System.out.println("Enter 10 integers for the second array:");
for (int i = 0; i < 10; i++)
{
b[i] = in.nextInt();
}
}
int sum(int x, int y)
{
return x + y;
}
int max(int x, int y)
{
return (x > y) ? x : y;
}
void display()
{
System.out.println("Sum and Maximum of corresponding elements:");
Adit Singhal 10 Ɣ
String pack_type : stores the type of packaging (e.g., tetra pack, PET bottle
Member methods:
Fruitjuice() : constructor to initialize integer data members to 0 and string data memebers
to “ “.
void input(): to input and store the product code, flavour, pack type, pack size and product
price
void discount(): to reduce the product price by 10
void display(): to display the product code, flavour, pack type, pack size and product price.
INPUT
import java.util.*;
class FruitJuice
{
int product_code, pack_size, product_price;
String pack_type, flavour;
FruitJuice()
{
pack_type="";
flavour="";
product_code=0;
pack_size=0;
product_price=0;
}
void input()
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Flavour: ");
flavour = in.nextLine();
System.out.print("Enter Pack Type: ");
pack_type = in.nextLine();
Adit Singhal 10 Ɣ
OUTPUT
Adit Singhal 10 Ɣ
}
public static void main()
{
BookFair ob=new BookFair();
ob.input();
ob.calculate();
ob.display();
}
}
OUTPUT