Java Assessment 2

You might also like

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

Java Assessment – 2

Name: Shamil Iqbal TP


Reg No: 18BCA0045

1. Create a class Product with member variables productID, productName,


Price, Quantity and dateofPurchase. Have a parameterized constructor to
initialize the productID, productName, dateofPurchase, Price and Quantity.

import java.util.Scanner;
class Product
{
int productID;
String productName;
int price;
int Quantity;
String dateofPurchase;
public Product(int pid,String pname,int pr,int quant,String date)
{
productID=pid;
productName=pname;
price=pr;
Quantity=quant;
dateofPurchase=date;
}
public static double calc(Product p)
{
Scanner sc=new Scanner(System.in);
double bill=p.Quantity*p.price;
double discount_amount=0.0;
if(bill>=2000&&bill<5000)
{
discount_amount=(bill*5)/100;
}
else if(bill>=5000)
{
discount_amount=((bill*49)/100);
}
if(discount_amount>(bill/2))
{
discount_amount=bill/2;
}
System.out.println("Discount amount is:"+discount_amount);
bill=bill-discount_amount;
return bill;
}
}

//Testproduct package

import java.util.Scanner;
class Testproduct
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int pid,price,qty;
String pname,dop;
double bill;
System.out.println("Enter product id");
pid=sc.nextInt();
System.out.println("Enter product name");
sc.nextLine();
pname=sc.nextLine();
System.out.println("Enter product price");
price=sc.nextInt();
System.out.println("Enter quantity of product purchased");
qty=sc.nextInt();
System.out.println("Enter date of purchase");
sc.nextLine();
dop=sc.nextLine();
Product p=new Product(pid,pname,price,qty,dop);
System.out.println("Product id:"+pid);
System.out.println("Product Name:"+pname);
System.out.println("Product Price:"+price);
System.out.println("Quantity of product purchased:"+qty);

System.out.println("Date of purchase:"+dop);
bill=Product.calc(p);

System.out.println("Total amount:"+bill);

}
}

2. Create a class NewsPaper by having member variables NPName,


No_of_pages, Date, Location., days. The newspaper cost Rs.2.45 perDay,
festival seasons like Pongal, Diwali newspapers cost is Rs. 3,00 and during
election time Rs.4.00,

import java.util.Scanner;
class sample{
public static void main(final String[] args) {
final Scanner sc = new Scanner(System.in);
Newspaper news =new Newspaper();
news.get();
news.print();
}
}

class Newspaper {
static String npname, date, location;
static int days, opt, no_of_pages = 10;
static double amount;
static int normal, festival, election;

static void get() {


final Scanner sc = new Scanner(System.in);
System.out.println("Enter the following");
System.out.print("\nNEWSPAPER NAME:");
npname = sc.next();
System.out.print("\nLOCATION:");
location = sc.next();
System.out.print("\nDATE:");
date = sc.next();
System.out.print("\nSelect package \n1.month\n2.weekends
only\n3.weekdays only\n4.year\nEnter option:");
opt = sc.nextInt();
}

static void payment(final int a) {


switch(a){
case 1:{
normal=27;
festival=3;
election=1;
days=31;
}break;
case 2:{
normal=99;
festival=4;
election=1;
days=104;
}break;
case 3:{
normal=250;
festival=6;
election=4;
days=260;
}break;
case 4:{
normal=350;
festival=10;
election=5;
days=365;
}break;
default:System.out.println("enter valid option");
break;
}
amount=(normal*2.45)+(festival*3)+(election*4);
if(a==4){
System.out.println("**YEARLY DISCOUNT 7% deducted**");
amount=amount-(amount/100*7);
}
}
static void print(){
System.out.print("\nNEWSPAPER SUBSCRIPTION DETAILS:");
System.out.println("\nNAME: "+npname);
System.out.println("NO.OF.PAGES: "+no_of_pages);
System.out.println("DATE: "+date);
System.out.println("LOCATION: "+location);
payment(opt);
System.out.println("AMOUNT TO BE PAID: "+amount);
System.out.println("DAYS: "+days);

}
}
3. Create a class Calculator with three private variables num1, num2 and total.
Have a parameterized constructor to initialize class member variables num1 and
num2. Have a common static method name called cal ( ) to perform addition,
subtraction, division and multiplication of its return type is int and a private
method called displayCal( ) to display operations.

import java.util.Scanner;
class calc{
public static void main( String[] args) {
final Scanner sc = new Scanner(System.in);
float num1,num2;
System.out.println("Enter numbers:");
System.out.print("num1:");
num1=sc.nextFloat();
System.out.print("num2:");
num2=sc.nextFloat();
calculator calc =new calculator();
calc.displaycal(num1,num2);
}
}
class calculator{
static int i=1;
static float res;
static String opt="Y";
protected static void displaycal(float n1,float n2){
final Scanner sc = new Scanner(System.in);
while(opt=="y"||opt=="Y"||i<=4){
System.out.println("press Y to continue or N to exit:");
opt=sc.next();
if(opt.equals("Y")||opt.equals("y")){
cal(i,n1,n2);
}
if(i==4){
opt="N";
}
i++;
}
System.out.println("HAVE A NICE DAY :)");
}
static void cal(int a,float b,float c){
switch(a){
case 1:res=b+c;
System.out.println("Sum of "+b+" and "+c+"is "+res);
break;
case 2:res=b-c;
System.out.println("Difference of "+b+" and "+c+"is "+res);
break;
case 3:res=b*c;
System.out.println("Product of "+b+" and "+c+"is "+res);
break;
case 4:res=b/c;
System.out.println("Division of "+b+" and "+c+"is "+res);
break;
}

}
}

4. Create class Shape with member variables width, height, breadth and angle.
a. Write a method to check whether the given shape is a quadrilateral.
b. Another method to check what type of quadrilateral.
– such as square, rectangle, parallelogram, rhombus and trapezium
c. Have a method to take input from user.

import java.util.Scanner;
class q4{
public static void main( String[] args) {
shape s=new shape();
s.get();
s.chk();
s.check();
s.display();
}
}
class shape{
static float width,height,breadth,angle,sangle;
static int sh;
void get(){
final Scanner sc = new Scanner(System.in);
System.out.print("WIDTH:");
width=sc.nextFloat();
System.out.print("HEIGHT:");
height=sc.nextFloat();
System.out.print("BREADTH:");
breadth=sc.nextFloat();
System.out.print("ANGLE:");
angle=sc.nextFloat();
System.out.println("enter sum of angles");
sangle=sc.nextFloat();
}
void chk(){
if(sangle==360){
System.out.println("it is a Quadrilateral");
}
}
void check(){
if(height==breadth && angle==90){
sh=1;//square
System.out.print("SQUARE:");
}
if (height!=breadth && angle==90){
sh=2;//rectangla
System.out.print("RECTANGLE:");
}
if (height==breadth && angle!=90){
sh=3;//rhombus
System.out.print("RHOMBUS:");
}
if (height!=breadth && angle!=90){
sh=4;//parallelogram
System.out.print("PARALLELOGRAM:");
}
if (angle!=90 && width!=height){
sh=5;//trapezium
System.out.print("TRAPEZIUM:");
}
}

void display(){
switch(sh){
case 1:System.out.println("A square has 4 equal sides and 4 right angles
at each corner.");break;
case 2:System.out.println("A rectangle has 4 sides with parallel sides are
of the same length and 4 corners are right angles. The rectangle has a longer
side and a shorter side");break;
case 3:System.out.println("A rhombus is like a square or diamond with 4
equal sides, but none of the corners is a right angle.");break;
case 4:System.out.println("Any quadrilateral that has a pair of parallel
sides is a parallelogram. There are shapes which demonstrate properties of
parallelogram – square, rectangle, and a rhombus are special case of a
parallelogram");break;
case 5:System.out.println("If a quadrilateral contains at least one pair of
parallel lines then it is called a trapezoid.");break;
}
}

5. Create a Cricket by having member variables such as playername, playerId,


specialization etc.

import java.util.Scanner;

public class Cricket {

public String playername,specialization;


int playerid;
float bspeed,rtime,hspeed;

public Cricket(){
Scanner scan =new Scanner(System.in);
System.out.println(" Enter the name of the Player : ");
playername = scan.nextLine();

System.out.println(" Enter the id of the player : ");


playerid = scan.nextInt();

System.out.println(" Enter the specialization : ");


specialization = scan.next();

System.out.println(" Enter the hit speed of the player : ");


hspeed = scan.nextFloat();
}

protected int cal() {

Scanner scan =new Scanner(System.in);

System.out.println(" Enter the speed of the ball : ");


bspeed = scan.nextFloat();

if((bspeed > 80) && (bspeed<=112))


rtime= (float) 0.91;
else if((bspeed > 112) && (bspeed<136))
rtime= (float)0.522;
else if((bspeed >= 137) && (bspeed<150))
rtime= (float)0.455;
else if((bspeed >= 150) && (bspeed<159))
rtime= (float)0.424;
else if((bspeed > 160) && (bspeed<161))
rtime= (float)0.395;

return 0;
}

public void chacal(float p1, float p2){

if(p1 > p2)


System.out.println(" the highest hit speed is done by player 1 : "+ p1);
else if (p2 > p1)
System.out.println(" The highest hit speed is done by player 2 : "+ p2);
else
System.out.println("THE draw");

public static void main(String[] args){

Cricket p1 = new Cricket();


Cricket p2 = new Cricket();

System.out.println(" When p1 bowls to p2 .....");


p1.cal();
System.out.println(" The bowling speed of the p1 : "+ p1.bspeed + " The
reaction time of the player 2 : "+ p1.rtime);
System.out.println(" When p2 bowls to p1 .....");
p2.cal();
System.out.println(" The bowling speed of the p1 : "+ p2.bspeed + " The
reaction time of the player 2 : "+ p2.rtime);

p1.chacal(p1.hspeed,p2.hspeed);

}
}

You might also like