Answers
Program code to copy
Fan.java
Fan.java public class Fan { public static final int SLOW=1,MEDIUM=2,FAST=3; // static final attributes with constant values private int speed; private boolean f_on; private double radius; private String color; Fan() { speed=SLOW; f_on=false; radius=5; color="blue"; } Fan(int speed,double radius,String color,boolean f_on) //Constructor to store values { this.speed=speed; this.radius=radius; this.color=color; this.f_on=f_on; } public void tostring() // toString method used to return String data of Fan { if(f_on==true) // Checking Whether Fan is on/off { System.out.println("Fan is on \n the speed is ="+speed+"\n the color is ="+color+"\n the radius is ="+radius); } else { System.out.println("Fan is off \n the color of fan is ="+color+"\n the radius of fan is ="+radius); } } public static void main(String[] args) { Fan obj1 = new Fan(FAST,10,"yellow",true); Fan obj2 = new Fan(MEDIUM,5,"blue",false); obj1.tostring(); obj2.tostring(); } }
Sample output
BankAccount.java
BankAccount.java import java.io.*; import java.util.*; class Bank { private String nameOfDepositor; private int accNum; private boolean accType; private double bal, AnnualInterestrate; void set(String n,int num,boolean acc,double B, double rate) { nameOfDepositor=n;accNum=num;accType=acc;bal=B;AnnualInterestrate=rate; } void deposit(double d) { bal+=d; } void withdraw(double w) { if(bal>=w && bal-w>50) bal-=w; else System.out.println("Insufficient Balance in your account"); } void disp() { System.out.println("Name of account Holder is :"+nameOfDepositor); System.out.println("Current Balance in your Savings Bank account is :"+bal ); } double Get_Monthly_interestRate() { AnnualInterestrate=((0.01*AnnualInterestrate)/12); return AnnualInterestrate; } void Get_Monthly_interest() { System.out.println("Monthly Interest for the available account balance is Rs. "+bal*Get_Monthly_interestRate()); } } public class BankAccount { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); Scanner sc=new Scanner(System.in); System.out.println("\n*******************WELCOME TO JAVA BANK********************"); System.out.print("Please enter your name :"); String s=in.readLine(); System.out.print("\nPlease enter your account number :"); int no=Integer.parseInt(in.readLine()); System.out.print("\nDo you have Savings Account at our branch?"); boolean ss=sc.nextBoolean(); if(ss==true) { System.out.println("You can proceed.... ;) "); } else { sc.close(); } System.out.print("\nEnter balance of your account: "); double bl=Double.parseDouble(in.readLine()); System.out.println("Enter Annual Interest Rate in Percentage: "); double r=sc.nextDouble(); Bank ob=new Bank(); ob.set(s,no,ss,bl,r); ob.disp(); System.out.print(" YOU WANT TO WITHDRAW MONEY(Y/N) : "); String w=in.readLine(); if(w.equalsIgnoreCase("y")) { System.out.print("\nEnter amount : "); double amnt=Double.parseDouble(in.readLine()); ob.withdraw(amnt); ob.disp(); ob.Get_Monthly_interest(); System.out.println("*******************THANKS FOR USING JAVA BANK*****************"); System.exit(0); } } }
sample output
stock_Q6.java
stock_Q6.java class Stock { private String symbol; private String name; private double previousClosingPrice; private double currentPrice; public Stock(String symbol, String name) { this.symbol = symbol; this.name = name; } public String getSymbol() { return symbol; } public void setSymbol(String symbol) { this.symbol = symbol; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getCurrentPrice() { return currentPrice; } public void setCurrentPrice(double currentPrice) { this.previousClosingPrice = this.currentPrice; this.currentPrice = currentPrice; } public double getPreviousClosingPrice() { return previousClosingPrice; } public void setPreviousClosingPrice(double previousClosingPrice) { this.previousClosingPrice = previousClosingPrice; } public double getChangePercent() { return (((currentPrice - previousClosingPrice) / previousClosingPrice)*100); } } public class stock_Q6 { public static void main(String[] args) { Stock stock1 = new Stock("ORCL", "Oracle Corporation"); stock1.setCurrentPrice(34.5); stock1.setCurrentPrice(34.35); System.out.println("Stock Name: " + stock1.getName() + " Symbol: " + stock1.getSymbol()); System.out.println("Previous Price: " + stock1.getPreviousClosingPrice()); System.out.println("Current Price: " + stock1.getCurrentPrice()); System.out.println("Percentage Changed: " + stock1.getChangePercent()); //Increase or Decrease in percentage as per the previous price and current price } }
sample output