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.*; import java.text.*; class CalculateHeight{
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("##.00"); Scanner input=new Scanner(System.in);
System.out.print("Enter constant rate:"); double g=input.nextDouble();
System.out.print("Enter time:"); double t=input.nextDouble(); double speed=g*t;
double h=speed*t; double height=h/2;
System.out.println("Rocket reaches at the height of: "+df.format(height)); }
}
[7]: Develop the program calculatePipeArea. It computes the
surface area of a pipe , which isan 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[] 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("Volume: "+(2*Math.PI*(r+t)*h)); } }
[8]: 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[] 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 x=rs/bs;
x=1+x*x; x=1/x; x=Math.sqrt(x); System.out.println("Distance: "+(w/x)); } }
[9]: 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 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))); } }
[10]: 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.
Solution:
static int output; // variables are defined at class level public static void deposit(int amount, int balance) // performs deposit calculation {
output = amount + balance; }
public static void withdraw(int amount, int balance) // performs withdraw calculation
{
output = balance - amount; if (balance >= amount) {
System.out.println(":::Inside the WithDraw method :::"); } else {
System.out.println(":::Inside the WithDraw method :::"); output = 0;
} }
public static void main(String[] args) {
int amount, balance; // local variables
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try {
System.out.println("\t\t BANK ::::::::");
System.out.println("Enter the Amount value: ");
amount = Integer.parseInt(in.readLine()); // Input value from keyboard for amount value
System.out.println("Enter the Balance value: ");
balance = Integer.parseInt(in.readLine()); // Input value from keyboard for balance value
deposit(amount, balance); // deposit method called for computation
System.out.println(":::Inside the Deposit method :::"); System.out.println("New Balance:::" + output);
withdraw(amount, balance); // withdraw method called for compu
System.out.println("New Balance:::" + output); } catch (Exception e) {
System.out.println("Error:::" + e); }
}
[11]: 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.
Solution:
import java.util.*; class Employee {
public double netSalary(double salary, double taxrate){ double tax=salary*taxrate;
double netpay=salary=tax; return netpay;
}
public static void main(String[] args) {
Employee emp=new Employee();
Scanner input=new Scanner(System.in); System.out.print("Enter Salary: "); double sal=input.nextDouble(); System.out.print("Enter Tax in %: "); double taxrate=input.nextDouble()/100; double net=emp.netSalary(sal,taxrate); System.out.println("Net Salary is: "+net); }
}
[12]: 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,double price){ this.productID=productID; this.name=name; this.categoryID=categoryID; this.price=price; } }
public class ElectricalProduct extends Product{ int voltageRange;
int wattage;
ElectricalProduct(int productID,String name,int categoryID,double price,int voltageRange, int wattage){
super(productID,name,categoryID,price); this.voltageRange=voltageRange;
this.wattage=wattage; }
public void display(){
System.out.println("Product ID: "+productID); System.out.println("Name: "+name);
System.out.println("Category ID: "+categoryID); System.out.println("Price: "+price);
System.out.println("Voltage Range: "+voltageRange); System.out.println("Wattage: "+wattage);
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in); System.out.println("Enter Product ID: "); int pid=input.nextInt();
System.out.println("Enter Name: "); String name=input.next();
System.out.println("Enter Catagory ID: "); int cid=input.nextInt();
System.out.println("Enter Price: "); double price=input.nextDouble();
System.out.println("Enter Voltage Range: "); int vrange=input.nextInt();
System.out.println("Enter Wattage: "); int wattage=input.nextInt();
System.out.println("****Details of Electrical Product****"); System.out.println(); ElectricalProduct p=new ElectricalProduct(pid,name,cid,price,vrange,wattage); p.display(); } }
[13]: 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 id; String title; String author; double price;
public void setId(int id){ this.id=id;
}
public int getId(){ return id; }
public void setTitle(String title){ this.title=title;
}
public String getTitle(){ return title;
}
public void setAuthor(String author){ this.author=author;
}
public String getAuthor(){ return author;
}
public void setPrice(double price){ this.price=price;
}
public double getPrice(){ return price; } } class Periodical { String timeperiod;
public void setTimeperiod(String timeperiod){ this.timeperiod=timeperiod;
}
public String getTimeperiod(){ return timeperiod;
} }
public class BookInformation{
public static void main(String[]args){ Book b=new Book();
Periodical p=new Periodical(); Scanner input=new Scanner(System.in); System.out.print("Book ID: ");
int id=input.nextInt(); b.setId(id); System.out.print("Title: "); String title=input.next(); b.setTitle(title); System.out.print("Author: "); String author=input.next(); b.setAuthor(author); System.out.print("Price: "); double price=input.nextDouble(); b.setPrice(price); System.out.print("Period: ");
String pp=input.next(); p.setTimeperiod(pp); System.out.println();
System.out.println("----Book Information----"); System.out.println();
System.out.println("Book ID: "+b.getId()); System.out.println("Title: "+b.getTitle()); System.out.println("Author: "+b.getAuthor()); System.out.println("Price: "+b.getPrice());
System.out.println("Period: "+p.getTimeperiod()); }
}