Java Assessment 2
Java Assessment 2
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);
}
}
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;
}
}
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;
}
}
import java.util.Scanner;
public Cricket(){
Scanner scan =new Scanner(System.in);
System.out.println(" Enter the name of the Player : ");
playername = scan.nextLine();
return 0;
}
p1.chacal(p1.hspeed,p2.hspeed);
}
}