0% found this document useful (0 votes)
16 views

Assignment Comp 1

Uploaded by

Adit Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Assignment Comp 1

Uploaded by

Adit Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Adit Singhal 10 Ɣ

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.*;

public class DiscountCalculator


{
public double Discount(double p, int a)
{

return p - (p * a / 100.0);
}
public double Discount(double p, int a, int b)
{
double p1 = p - (p * a / 100.0);

return p1- (p1* b / 100.0);


}
public double Discount(double p, int a, int b, int c)
{

double p1= p - (p * a / 100.0);


double p2= p1- (p1* b / 100.0);
return p2- (p2* c / 100.0);
}

public static void main()


{
DiscountCalculator dc = new DiscountCalculator();
Scanner in=new Scanner(System.in);

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();

double pf1 = dc.Discount(p,d1);


System.out.println("Final price after single discount: " + pf1);
System.out.println("Enter second discount: ");
d2=in.nextInt();

double pf2 = dc.Discount(p,d1,d2);


System.out.println("Final price after second discount: " + pf2);
System.out.println("Enter third discount: ");
d3=in.nextInt();

double pf3 = dc.Discount(p,d1,d2,d3);


System.out.println("Final price after third discount: " + pf3);
}
}
OUTPUT
Adit Singhal 10 Ɣ

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;

public class ArmstrongChecker


{
public int Armstrong(int n)
{

int originalNumber = n;
int sum = 0;
for (int t = n; t!= 0; t /= 10)
{
int digit = t % 10;

sum += Math.pow(digit, 3);


}
return (sum == originalNumber) ? 1 : 0;
}

public static void main()


{
Scanner scanner = new Scanner(System.in);
ArmstrongChecker ac = new ArmstrongChecker();

System.out.print("Enter a three-digit number: ");


int num = scanner.nextInt();
int r = ac.Armstrong(num);
if (r == 1)

{
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 Ɣ

Q8. Design a class to overload a method volume( ) as follows:


double volume(double r) — with radius (r) as an argument, returns the volume of sphere
using the formula: V = (4/3) * (22/7) * r * r * r

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;
}

public double volume(double h, double r)


{
return (22.0 / 7.0) * r * r * h;
}

public double volume(double l, double b, double h)


{
return l * b * h;
}

public static void main()


{
VolumeCalculator vc = new VolumeCalculator();
Scanner in=new Scanner(System.in);

int r,rc,h,hc,l,b;
System.out.println("Enter radius of sphere ");
r=in.nextInt();
Adit Singhal 10 Ɣ

double sphereVolume = vc.volume(r);


System.out.println("Volume of the sphere: " + sphereVolume);
System.out.println("Enter radius of cylinder ");

rc=in.nextInt();
System.out.println("Enter height of cylinder ");
hc=in.nextInt();
double cylinderVolume = vc.volume(rc, hc);

System.out.println("Volume of the cylinder: " + cylinderVolume);


System.out.println("Enter length of cuboid ");
l=in.nextInt();
System.out.println("Enter breath of cuboid ");

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 Ɣ

Q9. Design a class to overload a method Number() as follows:


(i) void Number (int num , int d) - To count and display the frequency of a digit in a
number.
Example:
num = 2565685
d=5
Frequency of digit 5 = 3
(ii) void Number (int n1) - To find and display the sum of even digits of a number.
Example:
n1 = 29865
Sum of even digits = 16
Write a main method to create an object and invoke the above methods.

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 Ɣ

System.out.print("Enter the rating of the movie (0.0 to 5.0): ");


rating = in.nextFloat();
if (rating < 0.0f || rating > 5.0f)
{
System.out.println("Invalid rating. Setting rating to 0.0.");
rating = 0.0f;
}
}
void display()
{
String message;
if (rating >= 0.0 && rating <= 2.0)
{
message = "Flop";
} else if (rating > 2.0 && rating <= 3.4)
{
message = "Semi-Hit";
} else if (rating > 3.4 && rating <= 4.5)
{
message = "Hit";
} else if (rating > 4.5 && rating <= 5.0)
{
message = "Super Hit";
} else
{
message = "Invalid Rating";
}
System.out.println("Movie Title: " + title);
System.out.println("Message: " + message);
}

public static void main() {


MovieMania movie = new MovieMania();
movie.accept();
movie.display();
}
}

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 Ɣ

bal = bal - amt;


System.out.println("Amount withdrawn: Rs. " + amt);
System.out.println("Balance: Rs. " + bal);
}
else
{
System.out.println("Insufficient Balance");
}
}
public void display()
{
System.out.println("Name: " + name);
System.out.println("Balance: Rs. " + bal);
}
}
OUTPUT
Adit Singhal 10 Ɣ

Q14. Write a program in java with the following

Specifications:

class name: Array

Data members: contain 10 integers. private int a[] and b[] to


Member methods
void getlist(): to accept the numbers in both the arrays
int sum (int x, int y): to find and return the sum of corresponding elements.
int max (int x, int y) : to find and return the maximum of the corresponding elements.
void display() : to display the sum as well as maximum of the corresponding elements of
both the arrays.

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 Ɣ

for (int i = 0; i < 10; i++)


{
int sumValue = sum(a[i], b[i]);
int maxValue = max(a[i], b[i]);
System.out.println("Pair " + (i + 1) + ": Sum = " + sumValue + ", Max = " + maxValue);
}
}
public static void main()
{
Array ob = new Array();
ob.getlist();
ob.display();
}
}
OUTPUT
Adit Singhal 10 Ɣ

Q15. Define a class named FruitJuice with the following description.

Instance variables/data members:

int product_code : stores the product code number

String flavour : Stores the flavour of juice (e.g., orange, apple)

String pack_type : stores the type of packaging (e.g., tetra pack, PET bottle

int pack_size : Stores package size (e.g,200ml,400ml)

int product_price : stores the price of the product.

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 Ɣ

System.out.print("Enter Product Code: ");


product_code = in.nextInt();
System.out.print("Enter Pack Size: ");
pack_size = in.nextInt();
System.out.print("Enter Product Price: ");
product_price = in.nextInt();
}
void discount()
{
product_price=product_price-10;
}
void display()
{
System.out.println("Enter product code: "+product_code);
System.out.println("Enter flavour: "+flavour);
System.out.println("Enter pack type: "+pack_type);
System.out.println("Enter pack size: "+pack_size);
System.out.println("Enter product price: "+product_price);
}
public static void main()
{
FruitJuice ob=new FruitJuice();
ob.input();
ob.discount();
ob.display();
}
}

OUTPUT
Adit Singhal 10 Ɣ

Q16. Define a class named BookFair with the following description:


instance variables/ data members:
String Bname - stores the name of the book
double price - stores the price of the book
member methods :
i. BookFair() - default constructor to initialize the data members.
ii. void input() - to input and store the name and the price of the book.
iii. void calculate() - to calculate the price after discount. Discount is calculated
based on the following criteria.
Price discount less than or equal to rs. 1000 2% of price
More than rs. 1000
Less than or equal to rs. 3000 10% of price more than rs. 3000 15% of price
INPUT
import java.util.*;
class BookFair
{
String Bname;
double price;
BookFair()
{
Bname="";
price=0.0;
}
void input()
{
Scanner in= new Scanner(System.in);
System.out.println("Enter name of book: ");
Bname=in.nextLine();
System.out.println("Enter price: ");
price=in.nextDouble();
}
void calculate()
{
if(price<=1000)
price=(98*price)/100;
if(price>1000 && price<=3000)
price=(90*price)/100;
if(price>3000)
price=(85*price)/100;
}
void display()
{
System.out.println("Enter name: "+Bname);
System.out.println("Enter price: "+price);
Adit Singhal 10 Ɣ

}
public static void main()
{
BookFair ob=new BookFair();
ob.input();
ob.calculate();
ob.display();
}
}
OUTPUT

You might also like