You are on page 1of 28

1. Write a program to find the difference between sum of the squares and the square of the sums of n numbers?

/*The purpose of this statement is to instruct the interpreter to load the Scanner class from the package java.*/ import java.util.Scanner; class Difference { public static void main(String args[]) { float s=0,s2=0; int i; System.out.print("Enter n numbers:"); Scanner st=new Scanner(System.in); int n=st.nextInt(); int a[] =new int[n]; for(i=0;i<n;i++) { a[i]=st.nextInt(); // Enter n numbers } for(i=0;i<n;i++) { s=s+a[i]; } float d=(s*s);// sum of the squares of n numbers System.out.print("\nSum of Squares of given "+n+" numbers is : "+d); for(i=0;i<n;i++) { s2=s2+((a[i])*(a[i]));// square of the sums of n numbers } System.out.print("\nSquares of Sum of given "+n+" numbers is : "+s2);

float f = (d-s2);// difference between sum of the squares and the square of the sums of n numbers System.out.println("\nDifference between sum of the squares and the square of the sums of "+n+" numbers is:" +f); } } OUTPUT:

D:\java>javac Difference.java D:\java>java Difference Enter n numbers:4 1234 Sum of Squares of given 4 numbers is : 100.0 Squares of Sum of given 4 numbers is : 30.0 Difference between sum of the squares and the square of the sums of 4 numbers is :70.0 2. Develop a program that accepts the area of a square and will calculate its perimeter. /*The purpose of this statement is to instruct the interpreter to load the Scanner class from the package java.*/ import java.util.Scanner; class Square_Area { public static void main(String args[]) { System.out.println("Enter the Area of a Square:"); Scanner ar=new Scanner(System.in);//Creating an object for the Scanner class double n=ar.nextDouble();// Getting the area of the Square. double s=Math.sqrt(n);//Calculating a side of the square using sqrt() function. double perimeter=4*s;//Calculating the perimeter of the Square.

System.out.println("The Perimeter of the given Square value is:"+perimeter); //Displaying the perimeter of the Square. } }

OUTPUT:

D:\java>javac Square_Area.java D:\java>java Square_Area Enter the Area of a Square: 144 The Perimeter of the given Square value is:48.0 3. Develop the program calculate Cylinder Volume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder.

import java.util.Scanner; import java.lang.*; class Cylinder_Volume { public static void main(String args[]) { double v=0; System.out.println(Enter the Radius of the Cylinder:); Scanner cs=new Scanner(System.in); //Getting the Radius of the Cylinder for the base
double r=cs.nextDouble(); System.out.println(Enter the Height of the Cylinder:); double h=st.nextDouble();//Getting the Height of the Cylinder double pie=3.14;// Value of pie=(22/7) or 3.14 v=pie*r*r*h;// FORMULA: Volume of the Cylinder System.out.println("The cylinder with radius="+r+" and height="+h+" has a volume of: "+v);// Displaying the Cylinder Volume.

} } OUTPUT:

D:\java> javac Cylinder_Volume.java D:\java>java Cylinder_Volume Enter the Radius of the Cylinder: 2 Enter the Height of the Cylinder: 4 The cylinder with radius=2.0 and height=4.0 has a volume of: 50.26548245743669 4. Utopias tax accountants always use programs that compute income taxes even though the tax rate is a solid, never-changing 15%. Define the program calculateTax which determines the tax on the gross pay. Define calculateNetPay that determines the net pay of an employee from the number of hours worked. Assume an hourly rate of $12. import java.util.*; public class Tax_Calculate

{ public static void main (String[] args) { double trate=0.15; double hrate=12; Scanner input=new Scanner(System.in); System.out.print("Enter Number of Days Worked in a year:"); int days=input.nextInt(); System.out.print("\nEnter Number of Working hours:"); double hrs=input.nextDouble(); double gpay=days*hrs*hrate; double tax=gpay*trate; double netpay=gpay-tax; System.out.print("Net Pay is: $"+netpay); } } OUTPUT: D:\java>javac Tax_Calculate.java D:\java>java Tax_Calculate Enter Number of Days Worked in a year:300 Enter Number of Working hours:12 Net Pay is: $36720.0 5. An old-style movie theater has a simple profit program. Each customer pays $5 per ticket. Every performance costs the theater $20, plus $.50 per attendee. Develop the program calculateTotalProfit that consumes the number of attendees (of a show) and calculates how much income the show earns. import java.util.*; public class Calculate_Income { public static void main(String[] args) {

Scanner s = new Scanner(System.in); System.out.print("Enter the no. of attendees of a show : "); int n=s.nextInt(); double profit = (n*5)-(20+(n*0.5)); System.out.println("\nTotal Profit of the theater per show (in $) is : " + profit); } } OUTPUT:

D:\java>javac Calculate_Income.java D:\java>java Calculate_Income Enter the no. of attendees of a show : 9 Total Profit of the theater per show (in $) is : 20.5 6. Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder.

import java.util.*;

public class Calculate_CylinderArea { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.print("Enter the base radius : "); double rad=s.nextDouble(); System.out.print("\nEnter the height : "); double ht=s.nextDouble(); double area=2*Math.PI*rad*(rad+ht);

System.out.println("\nSurface Area of the cylinder is : " + area); } } OUTPUT:

D:\java>javac Calculate_CylinderArea.java D:\java>java Calculate_CylinderArea Enter the base radius : 4 Enter the height : 9 Surface Area of the cylinder is : 326.7256359733385 7. Develop the program calculatePipeArea. It computes the surface area of a pipe, which is an open cylinder. The program accpets three values: the pipes inner radius, its length, and the thickness of its wall. import java.util.*; public class Calculate_PipeArea { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.print("Enter the inner radius : "); double rad=s.nextDouble(); System.out.print("\nEnter the length : "); double len=s.nextDouble(); System.out.print("\nEnter the thickness : "); double thick=s.nextDouble(); double area=2*Math.PI*(rad+thick)*len; System.out.println("\nSurface Area of the pipe is : " + area); } }

OUTPUT: D:\java>javac Calculate_PipeArea.java D:\java>java Calculate_PipeArea Enter the inner radius : 2 Enter the length : 6 Enter the thickness : 1 Surface Area of the pipe is : 113.09733552923255 8. Develop the program calculateHeight, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g t in t time units and a height of 1/2 * v * t where v is the speed at t. import java.util.*; public class Calculate_Height { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.print("Enter the time (in seconds) : "); double t=s.nextDouble(); double v=9.8*t; double height=0.5*v*t; System.out.println("\nHeight reached (in meters) is : " + height); } } OUTPUT:

D:\java>javac Calculate_Height.java D:\java>java Calculate_Height

Enter the time (in seconds) : 45 Height reached (in meters) is : 9922.500000000002 9. Develop a program that computes the distance a boat travels across a river, given the width of the river, the boat's speed perpendicular to the river, and the river's speed. Speed is distance/time, and the Pythagorean Theorem is c2 = a2 + b2. import java.util.*; public class Boat_Distance { public static void main(String[] args) { Scanner s= new Scanner(System.in); System.out.print("Enter the width of the river (in meters) : "); double rw=s.nextDouble(); System.out.print("\nEnter the river's speed (in meter/sec) : "); double rs=s.nextDouble(); System.out.print("\nEnter the boat's speed (in meter/sec) : "); double bs=s.nextDouble(); double time=rw/bs; //time takes to travel from shore to shore straight by the boat double w2=time*rs; //distance due to down stream double bd=Math.sqrt((rw*rw)+(w2*w2)); System.out.println("\nThe distance travelled by boat (in meters) is : "+bd); } } OUTPUT: D:\java>javac Boat_Distance.java D:\java>java Boat_Distance Enter the width of the river (in meters) : 50 Enter the river's speed (in meter/sec) : 40

Enter the boat's speed (in meter/sec) : 50 The distance travelled by boat (in meters) is : 64.03124237432849 10. Develop a program that accepts an initial amount of money (called the principal), a simple annual interest rate, and a number of months will compute the balance at the end of that time. Assume that no additional deposits or withdrawals are made and that a month is 1/12 of a year. Total interest is the product of the principal, the annual interest rate expressed as a decimal, and the number of years.

import java.util.*; public class Calculate_Balance { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.print("Enter the principal amount : "); double p=s.nextDouble(); System.out.print("\nEnter the annual interest rate : "); double r=s.nextDouble(); System.out.print("\nEnter the no. of months : "); double m=s.nextDouble(); double si=(p*(m/12)*r)/100; double bal=p+si; System.out.print("\nBalance after " +(int)m+" month(s) is : "+bal); } }

OUTPUT:

D:\java>javac Calculate_Balance.java D:\java>java Calculate_Balance Enter the principal amount : 20000 Enter the annual interest rate : 12 Enter the no. of months : 12

Balance after 12 month(s) is : 22400.0

1. Create a washing machine class with methods as switchOn, acceptClothes, acceptDetergent, switchOff. acceptClothes accepts the noofClothes as argument & returns the noofClothes import java.util.*; class WashingMachine { public void switchOn() { System.out.println ("The washing machine is switched on"); } public void acceptDetergent () { System.out.println("\nAccepting Detergent. "); } public int acceptClothes(int a) { return a; } public void switchOff () { System.out.println ("The washing machine is switched off."); } public static void main(String[] args) { WashingMachine wm=new WashingMachine(); wm.switchOn(); Scanner s=new Scanner(System.in); System.out.print("Enter no of clothes: "); int n=s.nextInt();

wm.acceptDetergent(); System.out.println("Finished washing of "+wm.acceptClothes(n)+" clothes"); wm.switchOff(); } } OUTPUT: D:\java>javac WashingMachine.java D:\java>java WashingMachine The washing machine is switched on Enter no of clothes: 10 Accepting Detergent. Finished washing of 10 clothes The washing machine is switched off. 2. Create a calculator class which will have methods add, multiply, divide & subtract I import java.util.*; class Calculator_Operations { public float add(float a,float b) { return a+b; } public float subtract(float a,float b) { return a-b; } public float multiply(float a,float b) { return a*b; } public float divide(float a,float b) { return a/b; } } public class Calculator { public static void main(String []args) { Calculator_Operations operation=new Calculator_Operations(); System.out.print("Enter two numbers:"); Scanner s=new Scanner(System.in); float num1=s.nextFloat(); float num2=s.nextFloat();

System.out.println("\nEnter choice:\n1.ADD\n2.SUBTRACT\n3.DIVIDE\n4.MULTIPLY\n"); int ch=s.nextInt(); switch(ch) { case 1:System.out.println("The result of Addition is: "+operation.add(num1,num2)); break; case 2:System.out.println("The result of Subraction is:"+operation.subtract(num1, num2)); break; case 3:System.out.println("The result of Division is: "+operation.divide(num1,num2)); break; case 4:System.out.println("The result of Multiplication is: "+operation.multiply(num1, num2)); break; } } } OUTPUT: D:\java>javac Calculator.java D:\java>java Calculator Enter two numbers:5 6 Enter choice: 1.ADD 2.SUBTRACT 3.DIVIDE 4.MULTIPLY 3 The result of Division is: 0.8333333 D:\java>java Calculator Enter two numbers:35 78 Enter choice: 1.ADD 2.SUBTRACT 3.DIVIDE 4.MULTIPLY 1 The result of Addition is: 113.0

D:\java>java Calculator Enter two numbers:56 78 Enter choice: 1.ADD 2.SUBTRACT 3.DIVIDE 4.MULTIPLY 2 The result of Subraction is: -22.0 D:\java>java Calculator Enter two numbers:09 0 Enter choice: 1.ADD 2.SUBTRACT 3.DIVIDE 4.MULTIPLY 4 The result of Multiplication is: 0.0 3. Create a class called Student which has the following methods: i. Average: which would accept marks of 3 examinations & return whether the student has passed or failed depending on whether he has scored an average above 50 or not. ii. Inputname: which would accept the name of the student & returns the name.

import java.util.*; class Calculate_Average { float avg; String name; public String average(float a,float b,float c) { avg=(a+b+c)/3; if(avg>=50) { return "PASSED";

} else return "FAILED"; } public String name(String str) { name=str; return name; } } class Student_Marks { public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.print("Enter the Student name: "); String n=s.nextLine(); System.out.print("\nEnter First subject marks: "); float num1=s.nextFloat(); System.out.print("\nEnter Second subject marks: "); float num2=s.nextFloat(); System.out.print("\nEnter Third subject marks: "); float num3=s.nextFloat(); Calculate_Average ct=new Calculate_Average(); String result=ct.average(num1,num2,num3); System.out.println("The Student "+ct.name(n)+" is "+result); } } OUTPUT:

D:\java>javac Student_Marks.java

D:\java>java Student_Marks Enter the Student name: Ashwin Enter First subject marks: 90 Enter Second subject marks: 99 Enter Third subject marks: 95 The Student Ashwin is PASSED 4. Create a Bank class with methods deposit & withdraw. The deposit method would accept attributes amount & balance & returns the new balance which is the sum of amount & balance. Similarly, the withdraw method would accept the attributes amount & balance & returns the new balance balance amount if balance > = amount or return 0 otherwise. import java.util.*; class Bank { public double deposit(double amt,double bal) { return (amt+bal); } public double withdraw(double amt,double bal) { if(bal>=amt) return (bal-amt); else return 0; } } class Bank_Atm { public static void main(String args[]) {

Bank b=new Bank(); Scanner s= new Scanner(System.in); System.out.print("Enter the amount to deposit: "); double a=s.nextDouble(); double bal=0; double newbal=b.deposit(a,bal); System.out.println("\nAmount is Deposited."); System.out.print("The total balance is: "+newbal); System.out.print("\nEnter the amount to withdraw: "); double am=s.nextDouble(); double ba=b.withdraw(am,newbal); if(ba==0) { System.out.println("NO sufficient balance to withdraw in your Account."); } else { System.out.println("The amount is withdrawn."); System.out.println("The balance is: "+ba); } } } OUTPUT:

D:\java>javac Bank_Atm.java D:\java>java Bank_Atm Enter the amount to deposit: 10000 Amount is Deposited. The total balance is: 10000.0

Enter the amount to withdraw: 2309 The amount is withdrawn. The balance is: 7691.0 5. Create an Employee class which has methods netSalary which would accept salary & tax as arguments & returns the netSalary which is tax deducted from the salary. Also it has a method grade which would accept the grade of the employee & return grade. import java.util.*; class Employee { public double netsalary(double sal,double tax) { return (sal-(sal*tax/100)); } public String grade(String empgrade) { return empgrade; } } class Tax_Employee { public static void main(String args[]) { Employee e=new Employee(); Scanner s = new Scanner(System.in); System.out.print("\nEnter grade of the Employee: "); String str=s.nextLine(); System.out.print("Enter the salary: "); double sa=s.nextDouble(); System.out.print("\nEnter tax (%): "); double t=s.nextDouble();

System.out.println("The net salary of "+e.grade(str)+" grade employee is: "+e.netsalary(sa,t)); } } OUTPUT:

D:\java>javac Tax_Employee.java D:\java>java Tax_Employee Enter grade of the Employee: y Enter the salary: 20000 Enter tax (%): 12 The net salary of y grade employee is: 17600.0 6. Create Product having following attributes: Product ID, Name, Category ID and UnitPrice. Create ElectricalProduct having the following additional attributes: VoltageRange and Wattage. Add a behavior to change the Wattage and price of the electrical product. Display the updated ElectricalProduct details.

import java.util.*; class Product { int productID; String name; int categoryID; double price; Product(int productID,String name,int categoryID) {

this.productID=productID; this.name=name; this.categoryID=categoryID; System.out.print("Enter the price of Bulb(per piece): "); Scanner s=new Scanner(System.in); this.price=s.nextDouble(); } public void display1() { System.out.print("\nProduct ID: "+productID); System.out.print("\nProduct name: "+name); System.out.print("\nCategory ID: "+categoryID); System.out.print("\nPrice: "+price); } } class ElectricalProduct extends Product { double voltageRange; double wattage; ElectricalProduct(int productID,String name,int categoryID,double voltageRange) { super(productID,name,categoryID); this.voltageRange=voltageRange; System.out.print("\nEnter Wattage of the Bulb(per piece): "); Scanner s=new Scanner(System.in); this.wattage=s.nextDouble(); } public void display() { super.display1();

System.out.println("\nVoltage Range: "+voltageRange+" Volts"); System.out.println("Wattage: "+wattage+" watts"); } } class Update_ElectricalProducts { public static void main(String args[]) { ElectricalProduct ep=new ElectricalProduct(006,"BULB",26,200); ep.display(); } } OUTPUT: D:\java>javac Update_ElectricalProducts.java D:\java>java Update_ElectricalProducts Enter the price of Bulb(per piece): 120 Enter Wattage of the Bulb(per piece): 40 Product ID: 6 Product name: BULB Category ID: 26 Price: 120.0 Voltage Range: 200.0 Volts Wattage: 40.0 watts

7. Create Book having following attributes: Book ID, Title, Author and Price. Create Periodical which has the following additional attributes: Period (weekly, monthly etc...) .Add a behavior to modify the Price and the Period of the periodical. Display the updated periodical details.

import java.util.*; class Book { int BookID=007; double Price=359.90; String Title="java"; String Author="GaryCornell"; } class Periodical extends Book { String Period="monthly"; public void display() { System.out.println("Book ID: "+BookID); System.out.println("\nPrice: "+Price); System.out.println("\nTitle: "+Title); System.out.println("\nAuthor: "+Author); System.out.println("\nperiod: "+Period); } } class Book_Period { public static void main(String args[]) { Periodical p=new Periodical(); Scanner s=new Scanner(System.in); System.out.print("Enter the period of book: "); p.Period= s.nextLine (); System.out.print("\nEnter the price of book: "); p.Price=s.nextInt(); System.out.println("\nThe Update Periodical details is Listed:\n"); p.display(); }

} OUTPUT: D:\java>javac Book_Period.java D:\java>java Book_Period Enter the period of book: YEARLY Enter the price of book: 320 The Update Periodical details is Listed: Book ID: 7 Price: 320.0 Title: java Author: GaryCornell period: YEARLY 8. Create Vehicle having following attributes: Vehicle No., Model, Manufacturer and Color. Create truck which has the following additional attributes:loading capacity( 100 tons).Add a behavior to change the color and loading capacity. Display the updated truck details. import java.util.*; class Vehicle { String vehno="TN22AZ3653"; String model="2012",manufacture="TATA",color="BLUE"; } class Truck extends Vehicle { String capacity;

public void display() { System.out.print("\nVehicle Number: "+vehno); System.out.print("\nModel: "+model); System.out.print("\nManufacturer: "+manufacture); System.out.print("\nColor : "+color); System.out.print("\nLoading Capacity: "+capacity+" Tons"); } } class Vehicle_Truck { public static void main(String args[]) { Truck p=new Truck(); Scanner s=new Scanner(System.in); System.out.print("Enter the color : "); p.color= s.nextLine (); System.out.print("\nEnter the loading capacity (TONS): "); p.capacity=s.nextLine(); System.out.println("\nThe Update Truck details is Listed:"); p.display(); } } OUTPUT:

D:\java>javac Vehicle_Truck.java D:\java>java Vehicle_Truck Enter the color : Violet Enter the loading capacity (TONS): 100 The Update Truck details is Listed:

Vehicle Number: TN22AZ3653 Model: 2012 Manufacturer: TATA Color : Violet Loading Capacity: 100 Tons 9. Write a program which performs to raise a number to a power and returns the value. Provide a behavior to the program so as to accept any type of numeric values and returns the results. import java.util.*; class Power { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter a number: "); double num=input.nextDouble(); System.out.print("\nEnter the number that "+num+" raised power number is: "); double pow=input.nextDouble(); double value=Math.pow(num,pow); System.out.println("The number "+num+" to the raised power "+pow+" is :"+value); } } OUTPUT: D:\java>javac Power.java D:\java>java Power Enter a number: 2 Enter the number that 2.0 raised power number is: 5 The number 2.0 to the raised power 5.0 is :32.0

D:\java>java Power Enter a number: 16 Enter the number that 16.0 raised power number is: 16 The number 16.0 to the raised power 16.0 is :1.8446744073709552E19 10. Write a function Model-of-Category for a Tata motor dealers, which accepts category of car customer is looking for and returns the car Model available in that category. the function should accept the following categories "SUV", "SEDAN", "ECONOMY", and "MINI" which in turn returns "TATA SAFARI" , "TATA INDIGO" , "TATA INDICA" and "TATA NANO" respectively. import java.util.*; class Car_Category { static String category_model(String Category) { String cat[]={"SUV","SEDAN","ECONOMY","MINI"}; String model[]={"TATA SAFARI","TATA INDIGO","TATA INDICA","NANO"}; String str=""; for(int i=0;i<4;i++) { if(Category.equals(cat[i])) { str=model[i]; break; } } return str; } public static void main(String args[]) { Car_Category Tata=new Car_Category(); System.out.println("1. SUV \n2. SEDAN \n3. ECONOMY \n4. MINI"); System.out.println("Choose any one of the category Model:\n");

Scanner s=new Scanner(System.in); String category=s.nextLine(); String choosen=Tata.category_model(category); System.out.println(choosen); } }

OUTPUT:

D:\java>javac Car_Category.java D:\java>java Car_Category 1. SUV 2. SEDAN 3. ECONOMY 4. MINI Choose any one of the category Model: MINI NANO D:\java>java Car_Category 1. SUV 2. SEDAN 3. ECONOMY 4. MINI Choose any one of the category Model: SEDAN TATA INDIGO D:\java>java Car_Category

1. SUV 2. SEDAN 3. ECONOMY 4. MINI Choose any one of the category Model: ECONOMY TATA INDICA D:\java> D:\java>java Car_Category 1. SUV 2. SEDAN 3. ECONOMY 4. MINI Choose any one of the category Model: SUV TATA SAFARI D:\java>java Car_Category 1. SUV 2. SEDAN 3. ECONOMY 4. MINI Choose any one of the category Model: asd

You might also like