java assignment 1

15
JAVA ASSIGNMENT 1 1. Write a program to find the difference between sum of the squares and the square of the sums of n numbers? Program: import java.io.*; import java.util.*; class DiffSumSqu { public static void main(String args[]) throws IOException { Scanner s=new Scanner(System.in); System.out.println("Enter the value of n: "); int n=s.nextInt(); int SumSqua=((n^3)/3)+((n^2)/2)+(n/6); int sum = 0; for(int i = n;i>=0;i--) { sum = sum + i; } int SquaSum = sum * sum; diff = SumSqua-SquaSum; System.out.println("Difference between sum of the squares and the square of the sum of given "+n);

Upload: sankar-narayanan

Post on 27-Nov-2014

868 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Assignment 1

JAVA ASSIGNMENT 1

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

Program:

import java.io.*;

import java.util.*;

class DiffSumSqu

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Enter the value of n: ");

int n=s.nextInt();

int SumSqua=((n^3)/3)+((n^2)/2)+(n/6);

int sum = 0;

for(int i = n;i>=0;i--){sum = sum + i;}int SquaSum = sum * sum;

diff = SumSqua-SquaSum;

System.out.println("Difference between sum of the squares and the square of the sum of given "+n);

System.out.print(" numbers is : "+diff);

System.out.println("");

}

Page 2: Java Assignment 1

}

Output:

Enter the value of n:

5

Difference between sum of the squares and the square of the sum of given 5 numbers is : 3328

2) Develop a program that accepts the area of a square and will calculate its perimeter.

Program:

import java.io.*;

import java.util.*;

class AreaPeri

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Area of Square : ");

double Area=s.nextDouble();

double Perimeter=4*Math.sqrt(Area);

System.out.print("so the Perimeter of the Square : "+p);

System.out.println("");

}

}

Page 3: Java Assignment 1

Output:

Area of Square :

16

so the Perimeter of the Square : 16.0

3)Develop the program calculateCylinderVolume., which accepts radius of a cylinder's base disk and its height and computes the volume of the cylinder.

Program:

import java.io.*;

import java.util.*;

class CalculateCylinderVolume

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

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

double rad=s.nextDouble();

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

double ht=s.nextDouble();

double vol=Math.PI*rad*rad*ht;

System.out.println("");

System.out.println("Volume of the cylinder is : " + vol);

}

Page 4: Java Assignment 1

}

Output :

Enter the radius :

2

Enter the height :

3

Volume of the cylinder is : 37.69911184307752

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.

Program:

import java.io.*;

import java.util.*;

class Utopias

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Days worked by employer in a year : ");

int days=s.nextInt();

System.out.println("Enter the no. of working hours in a day : ");

int hours=s.nextInt();

System.out.println("Enter the no. of hours worked in over time : ");

Page 5: Java Assignment 1

int ot=s.nextInt();

System.out.println("Enter the no. of hours took leave : ");

int l=s.nextInt();

double gross=((days*hours)+ot-l)*12;

double tax= gross*0.15;

double net=gross-tax;

System.out.println("");

System.out.println("Gross Pay (in $) : "+gross);

System.out.println("Net Pay (in $) : "+net);

System.out.println("Tax (in $) : "+tax);

}

}

Output :

Days worked by employer in a year :

300

Enter the no. of working hours in a day :

8

Enter the no. of hours worked in over time :

50

Enter the no. of hours took leave :

10

Gross Pay (in $) : 28800.0

Net Pay (in $) : 29280.0

Page 6: Java Assignment 1

Tax (in $) : 4320.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

Program:

import java.io.*;

import java.util.*;

class calculateTotalProfit

{

public static void main (String args[]) throws IOException

{

Scanner s = new Scanner(System.in);

System.out.println("Enter the no. of attendees of a show : ");

int n=s.nextInt();

double profit = (n*5)-(20+(n*0.5));

System.out.println("");

System.out.println("Total Profit of the theater per show (in $) is : " + profit);

}

}

Output :

Enter the no. of attendees of a show :

100

Total Profit of the theater per show (in $) is : 430.0

Page 7: Java Assignment 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.

Program:

import java.io.*;

import java.util.*;

class calculateCylinderArea

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Enter the base radius : ");

double rad=s.nextDouble();

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

double ht=s.nextDouble();

double area=2*Math.PI*rad*(rad+ht);

System.out.println("");

System.out.println("Surface Area of the cylinder is : " + area);

}

}

Formula : 2*pi*radius*radius+2*pi*radius+height = 2*pi*radius*(radius+height)

Output :

Enter the base radius :

2

Page 8: Java Assignment 1

Enter the height :

3

Surface Area of the cylinder is : 62.83185307179586

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.

Program:

import java.io.*;

import java.util.*;

class calculatePipeArea

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Enter the inner radius : ");

double radius=s.nextDouble();

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

double length=s.nextDouble();

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

double thick=s.nextDouble();

double area=2*Math.PI*(radius+thick)*lenght;

System.out.println("");

System.out.println("Surface Area of the pipe is : " + area);

Page 9: Java Assignment 1

}

}

Output :

Enter the inner radius :

3

Enter the length :

5

Enter the thickness :

2

Surface Area of the pipe is : 157.07963267948966

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.

Program:

import java.io.*;

import java.util.*;

class calculateHeight

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Enter the time (in seconds) : ");

double t=s.nextDouble();

Page 10: Java Assignment 1

double v=9.8*t;

double height=0.5*v*t;

System.out.println("");

System.out.println("Height reached (in meters) is : " + height);

}

}

Output :

Enter the time (in seconds) :

100

Height reached (in meters) is : 49000.00000000001

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.

Program:

import java.io.*;

import java.util.*;

class BoatDistance

{

public static void main(String args[]) throws IOException

{

Scanner s= new Scanner(System.in);

System.out.println("Enter the width of the river (in meters) : ");

double rw=s.nextDouble();

Page 11: Java Assignment 1

System.out.println("Enter the river's speed (in meter/sec) : ");

double rs=s.nextDouble();

System.out.println("Enter 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("");

System.out.println("The distance travelled by boat (in meters) is : "+bd);

}

}

Output :

Enter the width of the river (in meters) :

80

Enter the river's speed (in meter/sec) :

3

Enter the boat's speed (in meter/sec) :

4

The distance travelled by boat (in meters) is : 100.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.

Program:

Page 12: Java Assignment 1

import java.io.*;

import java.util.*;

class calculateBalance

{

public static void main(String args[]) throws IOException

{

Scanner s=new Scanner(System.in);

System.out.println("Enter the principal amount : ");

double p=s.nextDouble();

System.out.println("Enter the annual interest rate : ");

double r=s.nextDouble();

System.out.println("Enter the no. of months : ");

double m=s.nextDouble();

double si=(p*(m/12)*r)/100;

double bal=p+si;

System.out.println("");

System.out.print("Balance after " +(int)m);

System.out.println(" month(s) is : "+bal);

}

}

Output :

Enter the principal amount :

10000

Page 13: Java Assignment 1

Enter the annual interest rate :

6.5

Enter the no. of months :

6

Balance after 6 month(s) is : 10325.0