Java Assgn 1
Java Assgn 1
2. Develop a program that accepts the area of a square and will calculate its perimeter. 3. Develop the program calculateCylinderVolume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder. 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. 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. 6. Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder. 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. 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. 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. 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.
1. Write a program to find the difference between sum of the squares and the square of the sums of n numbers? import java.io.*; class sumofsquares
{ public static void main(String s[])throws IOException { int n; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of n:"); String a=br.readLine(); n=Integer.parseInt(a); int sum=n*(n+1)/2; System.out.println("the sum is " +sum); int sumsquares=n*(n+1)*(2*n+1)/6; System.out.println("the sum of sqaures is " +sumsquares); int squaresum=sum*sum; System.out.println("the square of sum is " +squaresum); int sos=(sum*sum)-sumsquares; System.out.println("Difference between sum of squares and squares of sum is "+sos); } }
2. Develop a program that accepts the area of a square and will calculate its perimeter.
import java.io.*; class perimetersquare { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the area of square:"); String area=br.readLine(); double areaofsquare=Double.parseDouble(area); double side=Math.sqrt(areaofsquare); System.out.println("the area of square is " +side); double perimeter=4*side; System.out.println("The perimeter of a square is "+perimeter); }}
3. Develop the program calculate CylinderVolume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder.
Solution : import java.io.*; class calculateCylinderVolume { public static void main(String s[])throws IOException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the radius(in inches):"); String r=br.readLine(); System.out.println("Enter the height(in inches):"); String h=br.readLine(); double pi=3.14159; double radius=Double.parseDouble(r); double height=Double.parseDouble(h); double volume=radius*radius*height*pi; System.out.println("The volume of a cylinder is "+volume); } }
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.
Solution : To Calculate Tax import java.io.*; class calculateTax { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the no of hours the employee worked:"); String a=br.readLine(); int n=Integer.parseInt(a); calculateTax c=new calculateTax(); int netpay=c.calculateNetPay(n); System.out.println("Net pay of the employee is "+netpay); int tax=15*netpay/100; System.out.println("Tax amount is "+tax); double grosspay=netpay-tax; System.out.println("Gross pay of the employee is "+grosspay); } int calculateNetPay(int i) { int h=12; int g=i*h;
return g; }}
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.
Solution : import java.io.*; class calculateTotalProfit { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader (System.in)); int theatercharge=20; double attendee=0.5; int ticket=5; System.out.println("Enter the no of customers:"); String n=br.readLine(); int number=Integer.parseInt(n); double attendeecharge=number*attendee; System.out.println("the attendee charge is " +attendeecharge); double income=(number*ticket)-(theatercharge+attendeecharge); System.out.println("Income the show earns is "+income); } }
6. Develop the program calculateCylinderArea, which accepts radius of the cylinder's base disk and its height and computes surface area of the cylinder.
Solution : import java.io.*; class calculateCylinderArea { public static void main(String s[])throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the radius(in inches):"); String r=br.readLine(); System.out.println("Enter the height(in inches):"); String h=br.readLine(); double pi=3.14159; double radius=Double.parseDouble(r); double height=Double.parseDouble(h); double surfacearea=(2*pi*radius*radius)+(2*pi*radius*height); System.out.println("The surface area of a cylinder is "+surfacearea); } }
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.
Solution: import java.io.*; class calculatePipeArea { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the inner radius of a pipe:"); String a=br.readLine(); System.out.println("Enter the length of the pipe:"); String b=br.readLine(); System.out.println("Enter the thickness of its wall:"); String c=br.readLine(); double r=Double.parseDouble(a); double l=Double.parseDouble(b); double t=Double.parseDouble(c); double pi=3.14159; double osa=pi*(2*r+t)*l; double isa=pi*(2*r)*l; double sa=(2*pi*(r+t)*(r+t))+(2*pi*r*r); double tsa=osa+isa+sa; System.out.println("Surface area of the given pipe is "+tsa); } }
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.
Solution: import java.io.*; class calculateHeight { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the time travelled by the rocket(in hrs):"); String h1=br.readLine(); int t=Integer.parseInt(h1); double g=9.8; double v=g*t; System.out.println("Speed of the rocket is "+v); double height=0.5*v*t; System.out.println("Height travelled by the rocket is "+height); } }
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.
Solution : import java.io.*; class distance { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the width of the river:"); String a1=br.readLine(); System.out.println("Enter the boat's speed:"); String b1=br.readLine(); System.out.println("Enter the river's speed:"); String c1=br.readLine();
int width=Integer.parseInt(a1); int bspeed=Integer.parseInt(b1); int rspeed=Integer.parseInt(c1); int a=width; int btime=a/bspeed; int b=btime*rspeed; int c=a*a+b*b; System.out.println("Distance travelled by the boat is "+c); } } 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.
Solution : import java.io.*; class compute { public static void main(String s[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the initial amount:"); String a=br.readLine(); System.out.println("Enter the annual interest rate:"); String b=br.readLine(); System.out.println("Enter the number of months:"); String c=br.readLine(); int p=Integer.parseInt(a); int n=Integer.parseInt(b); int r=Integer.parseInt(c); double interest=p*r*n/12; System.out.println("Interest amount is "+interest); double amount=p+interest; System.out.println("Balance is "+amount); } }