JAVA Assignment PART 1


/*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.io.*;
class calculateCylinderArea
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter radius”);
int r=Integer.parseInt(br.readLine());
System.out.println(“Enter height”);
int h=Integer.parseInt(br.readLine());
double area=2*Math.PI*r*h;
System.out.println(“Surface area of cylinder: “+area);
}
}
/* Output:
C:\>javac calculateCylinderArea.java

C:\>java calculateCylinderArea
Enter radius
4
Enter height
6
Surface area of cylinder: 150.79644737231007
*/

/*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.io.*;
class calculateHeight
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter time in seconds “);
int t=Integer.parseInt(br.readLine());
System.out.println(“Enter acceleration “);
Double g=Double.parseDouble(br.readLine());
double speed=g*t;
double Height=0.5*speed*t;
System.out.println(“Height: “+Height+” meters”);
}
}
/* Output:
C:\>javac calculateHeight.java

C:\>java calculateHeight
Enter time in seconds
5
Enter acceleration
9.8
Height: 122.5 meters */

/* 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.io.*;
class calculatePipeArea
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter inner radius”);
int r=Integer.parseInt(br.readLine());
System.out.println(“Enter thickness”);
int t=Integer.parseInt(br.readLine());
System.out.println(“Enter height”);
int h=Integer.parseInt(br.readLine());
System.out.println(“The surface area of a pipe: “+(2*Math.PI*(r+t)*h));
}
}
/*output:
C:\>javac calculatePipeArea.java

C:\>java calculatePipeArea
Enter inner radius
4
Enter thickness
2
Enter height
6
The surface area of a pipe: 226.1946710584651
*/

/*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.io.*;
class calculateTotalProfit
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter total no of attendees”);

int n=Integer.parseInt(br.readLine());
System.out.println(“Income the show earns: “+((5*n)));
System.out.println(“Total Income: “+((5*n)-(20+(0.5*n))));
}
}
/* Output:
C:\>javac calculateTotalProfit.java

C:\>java calculateTotalProfit
Enter total no of attendees
20
Income the show earns: 100
Total Income: 70.0
*/

/* 1. Write a program to find the difference between sum of the squares
and the square of the sums of n numbers?*/
class differ
{
public static void main(String[] args)
{
int diff=0;
int[] a =new int[args.length];

for(int i=0;i<args.length;i++)
{a[i]=Integer.parseInt(args[i]);}
int sosq=0;
for(int i=0;i<args.length;i++)
{sosq=sosq+(a[i]*a[i]);}
System.out.println("Sum of square is "+sosq);
int sqos=0;
for(int i=0;ijavac differ.java

C:\>java differ 1 2 3
Sum of square is 14
Square of sum is 36
difference between sum of the squares and the square of the sums of 3 numbers-22

/* 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.io.*;
class distance
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter boats speed”);
double bs=Double.parseDouble(br.readLine());
System.out.println(“Enter river speed”);
double rs=Double.parseDouble(br.readLine());
System.out.println(“Enter width of river”);
double w=Double.parseDouble(br.readLine());
double t=w/bs;
double b=t*rs;
double dist =b*b+w*w;
double distance = Math.sqrt (dist);
System.out.println(“The distance a boat travels across a river:”+distance);
}
}
/* Output:
C:\>javac distance.java

C:\>java distance
Enter boats speed
5
Enter river speed
5
Enter width of river
10
The distance a boat travels across a river:14.142135623730951 */

/* 2. Develop a program that accepts the area of a square and will calculate its perimeter. */
import java.io.*;
class Perim
{
public static void main(String[] args) throws IOException
{
double perim=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter area of square”);
int area=Integer.parseInt(br.readLine());
double side=Math.sqrt(area);
perim= 4 * side;
System.out.println(“Perimeter: “+perim);
}
}
/* Output:
C:\>javac Perim.java

C:\>java Perim
Enter area of square
36
Perimeter: 24.0 */

/*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.io.*;
class SimpleInterest
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter Principal”);
int p=Integer.parseInt(br.readLine());
System.out.println(“Enter Rate”);
double r=Double.parseDouble(br.readLine());
System.out.println(“Enter months”);
Double t=Double.parseDouble(br.readLine());
t=t/12;
System.out.println(“Interest: “+(0.01*(p*r*t)));
}
}
/* output:
C:\>javac SimpleInterest.java

C:\>java SimpleInterest
Enter Principal
1000
Enter Rate
10
Enter months
12
Interest: 100.0
*/

/*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.io.*;
class tax
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter total no of hours”);
int h=Integer.parseInt(br.readLine());
double tax_rate=0.15* (12 * h);
System.out.println(“:”+NetPay);
double NetPay =(12 *h)-tax_rate;
System.out.println(“NetPay:”+NetPay);
}
}
/* Output:
C:\>javac tax.java

C:\>java tax
Enter total no of hours
10
NetPay:102.0 */

/*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.*/
import java.io.*;
class Volume
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the radius of a cylinder’s base disk “);
int rad=Integer.parseInt(br.readLine());
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter the height “);
int height=Integer.parseInt(br1.readLine());
double volume= Math.PI*rad*rad*height;
System.out.println(“The volume of the cylinder:”+volume);
}
}
/*
Output:
C:\>javac Volume.java

C:\>java Volume
Enter the radius of a cylinder’s base disk
3
Enter the height
4
The volume of the cylinder:113.09733552923255
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: