TCS ASPIRE : JAVA ASSIGNMENT NO : 1


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

import java.util.Scanner;
import java.lang.String;
class sum
{
public static void main(String args[])
{
float s=0,s2=0;int i;
System.out.println(“enter n value”);
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 values
}
for(i=0;i<n;i++)
{
s=s+a[i];
}
float d=(s*s);
for(i=0;i<n;i++)
{s2=s2+((a[i])*(a[i]));}
float f = (d-s2);
System.out.println(f);
System.out.println(“difference between sum of the squares and the square of the sums of “+n+”numbers is:” +f);
}}
2. Develop a program that accepts the area of a square and will calculate its perimeter.
import java.util.Scanner;
import java.lang.*;
class area
{
public static void main(String args[])
{
System.out.println(“enter area value”);
Scanner st=new Scanner(System.in);
double n=st.nextDouble();
double s=Math.sqrt(n);
double perimeter=4*s;
System.out.println(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.
import java.util.Scanner;
import java.lang.*;
class calculateCylinderVolume
{
public static void main(String args[])
{
double v=0;
System.out.println(“enter radius value”);
Scanner st=new Scanner(System.in); double r=st.nextDouble();
System.out.println(“enter height”);
double h=st.nextDouble();
double p=Math.PI;
v=p*r*r*h;
System.out.println(“Volume of cylinder is: “+v);
}
}

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.*;
import java.text.*;
public class CalculateTax
{
public static void main (String[] args) {
double trate=0.15;
double hrate=12;
Scanner input=new Scanner(System.in);
System.out.println(“Enter Number of Days Worked in a year:”);
int days=input.nextInt();
System.out.print(“Enter Number of Working hours: “);
double hrs=input.nextDouble();
double gpay=days*hrs*hrate;
double tax=gpay*trate;
double netpay=gpay-tax;
System.out.println(“Net Pay is: $”+netpay);
}
}

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.*;
import java.util.Scanner;
class calculateTotalProfit
{
public static void main(String args[]) throws IOException
{
Scanner s=new Scanner(System.in);
System.out.println(“Enter the number of attendes for show : “);
int a=s.nextInt();
double percost = 20 ;
double attendecost =0.5;
double profit = (5*a)-(a*attendecost+20);
System.out.println(“The profit for the show is : $”+profit);
}
}
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.*;
import java.util.Scanner;
import java.lang.*;
class calculateCylinderArea
{
public static void main(String args[]) throws IOException
{
Scanner s=new Scanner(System.in);
System.out.println(“Enter the base radius of cylinder(in m): “);
double r=s.nextDouble();
System.out.println(“Enter the height of cylinder(in m) : “);
double h=s.nextDouble() ;
double pi=Math.PI;
double surfacearea=(2*pi*r*(r+h));
System.out.println(“The calculated surface area is: “+surfacearea+” sq m”);
}
}

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.*;
import java.util.Scanner;
import java.lang.*;
class calculatePipeArea
{
public static void main(String args[]) throws IOException
{
Scanner s=new Scanner(System.in);
System.out.println(“Enter the inner radius of pipe(in m): “);
double r=s.nextDouble();
System.out.println(“Enter the length of pipe(in m) : “);
double l=s.nextDouble() ;
System.out.println(“Enter the thickness of pipe(in m) : “);
double t=s.nextDouble() ;
double pi=Math.PI;
double surfacearea=(2*pi*(r+t)*l);
System.out.println(“The calculated surface area is: “+surfacearea+” sq m”);
}
}

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.Scanner;
public class calculateHeight {
public static void main(String[] args) {
System.out.println(“Enter the time in seconds : “);
Scanner s=new Scanner(System.in);
double time=s.nextDouble();
double acc;
double g=9.8;
acc=g;
double vel=acc*time;
double h=(vel*time)/2;
System.out.println(“Height reached by the rocket (in km) is : ” +(h/1000));
}
}

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.Scanner;
public class Distance{
public static void main(String[] args)
{
Scanner s= new Scanner(System.in);
System.out.println(“Enter the width of the river (in m) : “);
double width=s.nextDouble();
System.out.println(“Enter speed of river (in m/s) : “);
double rspeed=s.nextDouble();
System.out.println(“Enter speed of the boat(in m/s) : “);
double bspeed=s.nextDouble();
double time=width/bspeed; //time to travel to other bank by the boat
double d=time*rspeed; //distance travelled down stream
double totald=Math.sqrt((width*width)+(d*d));
System.out.println(“The distance travelled by boat(in m): “+totald);
}
}

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.Scanner;
public class calculateBalance {
public static void main(String[] args)
{
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 in percentage: “);
double ir=s.nextDouble();
System.out.println(“Enter the number of months : “);
double m=s.nextDouble();
double ad=p*(m/12)*(ir/100);
double finalbal=p+ad;
System.out.print(“final balance will be “+finalbal);
}
}


1 comment:

  1. Define constructors and getter setters as given in the class outline. Ensure attributes are private and other methods which will be called from main method,
    getter-setter methods and constructor is public.

    Declare Bank class with below attributes:
    a. Balance – float
    Write below methods in this class.
    depositMoney(depostAmount) // adds amount to balance
    withdrawMoney(withdrawAmount) // subtracts amount from balance
    calculateInterest(interestPercentage) // calculates interest and adds to balance
    displayDetails() //displays the balance details

    ReplyDelete