quest 1 define a class batsman with the following specifications

58
Quest 1 Define a class batsman with the following specifications: Private members: bcode 4 digits code number bname 20 characters innings, notout, runs integer type batavg it is calculated according to the formula – batavg =runs/(innings- notout) calcavg() Function to compute batavg Public members: readdata() Function to accept value from bcode, name, innings, & notout and invoke the function calcavg() displaydata() Function to display the data members on the screen. import java.util.Scanner; class batsman { private int bcode; private String bname; private int innings,notout,runs; double batavg; private void batavg() {

Upload: rajkumari873

Post on 14-Apr-2017

682 views

Category:

Data & Analytics


2 download

TRANSCRIPT

Page 1: Quest  1 define a class batsman with the following specifications

Quest 1 Define a class batsman with the following specifications:Private members:bcode                            4 digits code numberbname                           20 charactersinnings, notout, runs        integer typebatavg                           it is calculated according to the formula –                                      batavg =runs/(innings-notout)calcavg()                         Function to compute batavgPublic members:readdata()                      Function to accept value from bcode, name, innings, & notout and invoke the function  calcavg()displaydata()                 Function to display the data members on the screen.

import java.util.Scanner;

class batsman

{

private int bcode;

private String bname;

private int innings,notout,runs;

double batavg;

private void batavg()

{

}

private void calcavg()

{

batavg =runs/(innings-notout);

Page 2: Quest  1 define a class batsman with the following specifications

}

public void readdata()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter the batsman code: ");

bcode=in.nextInt();

System.out.print("Enter the batsman name: ");

bname=in.next();

System.out.print("Enter the number of innings: ");

innings=in.nextInt();

System.out.print("Enter the number of not out: ");

notout=in.nextInt();

System.out.print("Enter the batsman runs: ");

runs=in.nextInt();

calcavg();

}

public void displaydata()

{

System.out.println("batsman code: " +bcode);

System.out.println("batsman name: " +bname);

System.out.println("number of innings: " +innings);

System.out.println("number of not out: " +notout);

System.out.println("batsman runs: " +runs);

System.out.println("batsman average: " +batavg);

Page 3: Quest  1 define a class batsman with the following specifications

}

}

class batman

{

public static void main(String args[])

{

batsman b=new batsman();

b.readdata();

b.displaydata();

}

}

Ques 2. Define a class TEST in JAVA with following description:Private MembersTestCode of type integerDescription of type stringNoCandidate of type integerCenterReqd (number of centers required) of type integer

Page 4: Quest  1 define a class batsman with the following specifications

A member function CALCNTR() to calculate and return the number of centers as(NoCandidates/100+1)Public Members -  A function SCHEDULE() to allow user to enter values for TestCode, Description, NoCandidate & call function CALCNTR() to calculate the number of Centres- A function DISPTEST() to allow user to view the content of all the data members

import java.util.Scanner;

class Test {

private int testcode;

private String Des;

private int nocandi;

private int calcntr()

{

return (nocandi/100+1);

}

public void schedule()

{

Scanner s = new Scanner(System.in);

System.out.print("Enter the testcode values:");

testcode = s.nextInt();

System.out.print("Enter the description:");

Des = s.next();

System.out.print("No. of candidate:");

nocandi = s.nextInt();

calcntr();

}

public void disptest()

{

Page 5: Quest  1 define a class batsman with the following specifications

System.out.println("testcode :" +testcode);

System.out.println("description is :" +Des);

System.out.println("no. of centers is :" +nocandi);

}

}

class testt

{

public static void main(String[] args) {

Test t1 = new Test();

t1.schedule();

t1.disptest();

}

}

Quest 3. Define a class in C++ with following description:Private MembersA data member Flight number of type integer

Page 6: Quest  1 define a class batsman with the following specifications

A data member Destination of type stringA data member Distance of type floatA data member Fuel of type floatA member function CALFUEL() to calculate the value of Fuel as per the following criteria            Distance                                                          Fuel            <=1000                                                           500            more than 1000  and <=2000                          1100            more than 2000                                              2200Public MembersA function FEEDINFO() to allow user to enter values for Flight Number, Destination, Distance & call function CALFUEL() to calculate the quantity of FuelA function SHOWINFO() to allow user to view the content of all the data members

import java.util.Scanner;

class flight

{

private int flight_no;

private String destination;

private float distance;

private float fuel;

private void calfuel()

{

if (distance<=1000)

fuel=500;

else if(distance>=2000)

fuel=1000;

else fuel= 2200;

Page 7: Quest  1 define a class batsman with the following specifications

}

public void FEEDINFO()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter the Flight number: ");

flight_no=in.nextInt();

System.out.print("Enter the Destination of flight: ");

destination=in.next();

System.out.print("Enter the Distance of Flight: ");

distance=in.nextFloat();

calfuel();

}

public void SHOWINFO()

{

System.out.println("Flight number= " +flight_no);

System.out.println("Destination of flight= " +destination);

System.out.println("Distance of Flight= " +distance);

System.out.println("Fuel= " +fuel);

}

}

class flighttest

{

public static void main(String args[])

{

flight ft=new flight();

Page 8: Quest  1 define a class batsman with the following specifications

ft.FEEDINFO();

ft.SHOWINFO();

}

}

Quest 4. Define a class BOOK with the following specifications :Private members of the class BOOK areBOOK NO                integer typeBOOKTITLE             20 charactersPRICE                     float (price per copy)TOTAL_COST()       A function to calculate the total cost for N number of copies

where N is passed to the function as argument. Public members of the class BOOK areINPUT()                   function to read BOOK_NO. BOOKTITLE, PRICEPURCHASE()            function to ask the user to input the number of copies to be

purchased. It invokes TOTAL_COST() and prints the total

Page 9: Quest  1 define a class batsman with the following specifications

cost to be paid by the user. import java.util.Scanner;

class BOOK

{

private int book_no,no_copies;

private String book_title;

private float price,cost;

private void TOTAL_COST(int n)

{

cost=n*price;

}

public void input()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter the Book Number: ");

book_no=in.nextInt();

System.out.print("Enter the Book Title: ");

book_title=in.next();

System.out.print("Enter the Book Price: ");

price=in.nextFloat();

}

public void purchase()

{

Scanner in=new Scanner(System.in);

Page 10: Quest  1 define a class batsman with the following specifications

System.out.print("enter the no of copies purchase:");

no_copies=in.nextInt();

TOTAL_COST(no_copies);

}

public void display()

{

System.out.println("Book Number: "+book_no);

System.out.println("Book Title: "+book_title);

System.out.println("Book Price: "+price);

System.out.println("Number of copies: "+no_copies);

System.out.println("total Cost " +cost);

}

}

class books

{

public static void main(String args[])

{

BOOK b= new BOOK();

b.input();

b.purchase();

b.display();

Page 11: Quest  1 define a class batsman with the following specifications

}

}

Quest 5. Define a class REPORT with the following specification:Private members :adno                         4 digit admission numbername                        20 charactersmarks                       an array of 5 floating point valuesaverage                    average marks obtainedGETAVG()                 a function to compute the average obtained in five subjectPublic members:READINFO()              function to accept values for adno, name, marks. Invoke the function GETAVG()             DISPLAYINFO()          function to display all data members of report on the screen.

import java.util.*;

class Report

{

Page 12: Quest  1 define a class batsman with the following specifications

private int adno;

private String name;

private double marks[]={56.00,68.50,69.30,76.00,78.30};

private double avg;

private double getavg()

{

return((56.00+68.50+69.30+76.00+78.30)/5.00);

}

public void readinfo()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter 4 digit addmission number:");

adno=in.nextInt();

System.out.print("Enter name:");

name=in.next();

}

public void displayinfo()

{

System.out.println("The 4 digit admission number is:"+adno);

System.out.println("The name of the student is:"+name);

System.out.println("The avg in five subjects obtained by the student is:"+getavg());

}

}

class reportt

{

Page 13: Quest  1 define a class batsman with the following specifications

public static void main(String[] args)

{

Report obj=new Report();

obj.readinfo();

obj.displayinfo();

}

}

Q6. Define a class student with the following specificationPrivate members of class studentadmno integersname 20 charactereng. math, science floattotal floatctotal() a function to calculate eng + math + science with float return type.Public member function of class studentTakedata() Function to accept values for admno, sname, eng, science and invoke ctotal() to calculate total.Showdata() Function to display all the data members on the screen

import java.util.Scanner;

Page 14: Quest  1 define a class batsman with the following specifications

class Student

{

private int adno;

private String sname;

private float eng,math,sci;

private float total;

public void ctotal()

{

total=eng+math+sci;

}

public void takedata()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter the Admission number: ");

adno=in.nextInt();

System.out.print("Enter the Name: ");

sname=in.next();

System.out.print("Enter the English Marks: ");

eng=in.nextFloat();

System.out.print("Enter the Math Marks: ");

math=in.nextFloat();

System.out.print("Enter the Science Marks: ");

sci=in.nextFloat();

ctotal();

}

Page 15: Quest  1 define a class batsman with the following specifications

public void showdata()

{

System.out.println("Addmission Number:" +adno);

System.out.println("English marks:" +eng);

System.out.println("Math marks:" +math);

System.out.println("Science marks:" +sci);

System.out.println("Total marks:" +total);

}

}

class studentt

{

public static void main(String[] args)

{

Student s1=new Student();

s1.takedata();

s1.showdata();

}

}

Page 16: Quest  1 define a class batsman with the following specifications

Q7. Define a class TelephoneData Members :intprv,pre - to store the previous and present meter readingint call - to store call made (pre-prv)String name - to store name of the customerdoubleamt - to store amount double total - to store total amountmember Methodsvoid input() -to get the value for previous, present reading and the

name of the customervoidcal() - to calculate amount and total amount void display() -to display the name , calls made, amount and total

amount to be paid in the following format.

Name Calls Made Amount Total Amount-------- ------------ ----------- ------------------

Calls made RateUpto 100 calls No chargeFor the next 100 calls 90 paisa per call

Page 17: Quest  1 define a class batsman with the following specifications

For the next 200 calls 80 paisa per callMore than 400 calls 70 paisa per call

Every customer has to pay Rs. 180 per month as monthly rent for the services.

import java.util.Scanner;

class Telephone

{

int prv,pre;

int call;

String name;

double amt;

double total;

void input()

{

Scanner s = new Scanner(System.in);

System.out.println("Enter the Customer name :");

name = s.next();

System.out.println("Enter the present meter reading");

pre = s.nextInt();

System.out.println("Enter the previous meter reading");

prv = s.nextInt();

System.out.println("total calls is :" );

call = (pre-prv);

}

Page 18: Quest  1 define a class batsman with the following specifications

void cal()

{

if(call<100)

{

System.out.println("charge rupee 180/- for montly services :");

System.out.println("no other charge ** Enjoy**");

amt = 0.0;

total = 180.0;

}

else if(call<=200)

{

System.out.println("charge rupee 180/- for montly services :");

System.out.println("charges is :" );

amt = 100*0.90;

total = 180+amt;

}

else if(call<=400)

{

amt = (100*0.90)+(200*0.80);

total = 180+amt;

}

else

{

amt = 0.40*call;

Page 19: Quest  1 define a class batsman with the following specifications

total=180+amt;

}

}

void display()

{

System.out.println("Name of the customer :" +name);

System.out.println("total calls : " +call);

System.out.println("amount without tax :" +amt);

System.out.println("Total amount :" +total);

}

}

class telephonee

{

public static void main(String[] args)

{

Telephone t = new Telephone();

t.input();

t.cal();

t.display();

}

}

Page 20: Quest  1 define a class batsman with the following specifications

Q8. (The Triangle class) Design a class named Triangle that extendsGeometricObject. The class contains:■ Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle.■ A no-arg constructor that creates a default triangle.■ A constructor that creates a triangle with the specified side1, side2, and side3.■ The accessor methods for all three data fields.■ A method named getArea() that returns the area of this triangle.■ A method named getPerimeter() that returns the perimeter of this triangle.■ A method named toString() that returns a string description for the triangle.The toString() method is implemented as follows:return "Triangle: side1 = " + side1 + " side2 = " + side2 +" side3 = " + side3;For the formula to compute the area of a triangle. Implement the class. Write a test program that creates a Triangle object with sides 1, 1.5, 1, color yellow and filled true, and displays the area, perimeter, color, and whether filled or not.

class GeometricObject

{

private String color = "Yellow";

private Boolean filled = true;

Page 21: Quest  1 define a class batsman with the following specifications

public String getColor()

{

return color;

}

public void setColor(String color)

{

this.color = color;

}

public boolean isFilled()

{

return filled;

}

public void setFilled(boolean filled)

{

this.filled = filled;

}

public String toStrings()

{

return " Color: " + color + " and filled: " + filled;

Page 22: Quest  1 define a class batsman with the following specifications

}

}

class Triangle extends GeometricObject

{

private double side1 = 1.0;

private double side2 = 1.0;

private double side3 = 1.0;

public Triangle()

{}

public Triangle (double side1, double side2, double side3)

{

this.side1=side1;

this.side2=side2;

this.side3=side3;

}

public double getSide1()

{

return side1;

}

Page 23: Quest  1 define a class batsman with the following specifications

public double setSide2()

{

return side2;

}

public double setSide3()

{

return side3;

}

public void setSide1 (double side1)

{

this.side1=side1;

}

public void setSide2 (double side2)

{

this.side2=side2;

}

public double getArea()

{

return (side1+side2+side3)/2;

}

Page 24: Quest  1 define a class batsman with the following specifications

public double getPerimeter()

{

return side1+side2+side3;

}

public String toString()

{

return "Side 1 = " + side1 + " Side 2 = " + side2 + " Side 3 = " + side3;

}

}

public class trianglee

{

public static void main (String [ ] args)

{

Triangle Triangle = new Triangle(1,1.5,1);

System.out.println("The Triangle Sides are " + Triangle.toString());

System.out.println("The Triangle’s Area is " + Triangle.getArea());

System.out.println("The Triangle’s Perimeter is " + Triangle.getPerimeter());

System.out.println("The Triangle's Color is " + Triangle.getColor());

System.out.println("The Triangle is " + Triangle.isFilled());

}

}

Page 25: Quest  1 define a class batsman with the following specifications

Q9. (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person’s name. Implement the classes. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.

import java.util.Scanner;

class mydate

{

int d,m,y;

public void getdate()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter day: ");

d=in.nextInt();

Page 26: Quest  1 define a class batsman with the following specifications

System.out.print("Enter month: ");

m=in.nextInt();

System.out.print("Enter Year: ");

y=in.nextInt();

}

public void putdate()

{

System.out.println("Day: " +d);

System.out.println("Month: " +m);

System.out.println("Year: " +y);

}

}

class Person

{

String name,address,email_address;

int phno;

public void getdata()

{

Scanner in=new Scanner(System.in);

System.out.print("Enter the name of a person: ");

name=in.nextLine();

System.out.print("Enter the address of a person: ");

address=in.nextLine();

System.out.print("Enter the phone number: ");

Page 27: Quest  1 define a class batsman with the following specifications

phno=in.nextInt();

System.out.print("Enter the email address: ");

email_address=in.next();

}

public void to_String()

{

System.out.println("Name= " +name);

System.out.println("Address= " +address);

System.out.println("Phno= " +phno);

System.out.println("Email address= " +email_address);

}

}

class Student extends Person

{

public String status;

public void getdata()

{

super.getdata();

Scanner in=new Scanner(System.in);

System.out.print("Enter the status: ");

status=in.nextLine();

}

Page 28: Quest  1 define a class batsman with the following specifications

public void to_String()

{

super.to_String();

System.out.println("Status= " +status);

}

}

class Employee extends Person

{

String office;

int salary;

mydate hired_date=new mydate();

public void getdata()

{

super.getdata();

Scanner in=new Scanner(System.in);

System.out.print("Enter office: ");

office=in.next();

System.out.print("Enter salary: ");

salary=in.nextInt();

System.out.print("Enter hiredate: ");

hired_date.putdate();

}

public void to_String()

{

Page 29: Quest  1 define a class batsman with the following specifications

super.to_String();

System.out.println("Office= " +office);

System.out.print("Salary= " +salary);

hired_date.putdate();

}

}

class faculty extends Employee

{

int rank;

float office_hrs;

public void getdata()

{

super.getdata();

Scanner in=new Scanner(System.in);

System.out.print("Enter rank: ");

rank=in.nextInt();

System.out.print("Enter office hours: ");

office_hrs=in.nextFloat();

}

public void to_String()

{

super.to_String();

System.out.println("Rank= " +rank);

System.out.println("Office hours= " +office_hrs);

Page 30: Quest  1 define a class batsman with the following specifications

}

}

class staff extends Employee

{

String title;

public void getdata()

{

super.getdata();

Scanner in=new Scanner(System.in);

System.out.println("Enter title: ");

title=in.next();

}

public void to_String()

{

super.toString();

System.out.println("title= " +title);

}

}

class testtt

{

public static void main(String[] args)

{

Scanner in=new Scanner(System.in);

Page 31: Quest  1 define a class batsman with the following specifications

int ch;

System.out.println("Enter the Information");

System.out.println("\n1.Student\n2.faculty\n3.staff\n");

System.out.print("Enter the choice: ");

ch=in.nextInt();

switch(ch)

{

case 1:

Student s=new Student();

s.getdata();

s.to_String();

break;

case 2:

faculty f=new faculty();

f.getdata();

f.to_String();

break;

case 3:

staff st=new staff();

st.getdata();

st.to_String();

break;

default:

System.out.println("No information available");

}

Page 32: Quest  1 define a class batsman with the following specifications

}

}

Q.19. Write a program using AWT/swings or create a GUI in application for a simple calculator. (use proper layout).import java.awt.*;import java.awt.event.*;import javax.swing.*;public class calculator extends JApplet { public static void main(String[] args) { JFrame window = new JFrame("Simple Calculator"); CalcPanel content = new CalcPanel(); window.setContentPane(content); window.pack(); window.setLocation(100,100); window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); window.setVisible(true); } public void init() { setContentPane( new CalcPanel() ); } public static class CalcPanel extends JPanel implements ActionListener { private JTextField xInput, yInput;

Page 33: Quest  1 define a class batsman with the following specifications

private JLabel answer; public CalcPanel() { setBackground(Color.GRAY); setBorder( BorderFactory.createEmptyBorder(5,5,5,5) ); xInput = new JTextField("0", 10); xInput.setBackground(Color.WHITE); yInput = new JTextField("0", 10); yInput.setBackground(Color.WHITE); JPanel xPanel = new JPanel(); xPanel.add( new JLabel(" x = ")); xPanel.add(xInput); JPanel yPanel = new JPanel(); yPanel.add( new JLabel(" y = ")); yPanel.add(yInput); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(1,4)); JButton plus = new JButton("+"); plus.addActionListener(this); buttonPanel.add(plus); JButton minus = new JButton("-"); minus.addActionListener(this); buttonPanel.add(minus); JButton times = new JButton("*"); times.addActionListener(this); buttonPanel.add(times); JButton divide = new JButton("/"); divide.addActionListener(this); buttonPanel.add(divide); answer = new JLabel("x + y = 0", JLabel.CENTER); answer.setForeground(Color.red); answer.setBackground(Color.white); answer.setOpaque(true); setLayout(new GridLayout(4,1,3,3)); add(xPanel); add(yPanel); add(buttonPanel); add(answer); xInput.requestFocus(); } public void actionPerformed(ActionEvent evt) { double x, y; try { String xStr = xInput.getText(); x = Double.parseDouble(xStr); }

Page 34: Quest  1 define a class batsman with the following specifications

catch (NumberFormatException e) { answer.setText("Illegal data for x."); xInput.requestFocus(); return; } try { String yStr = yInput.getText(); y = Double.parseDouble(yStr); } catch (NumberFormatException e) { answer.setText("Illegal data for y."); yInput.requestFocus(); return; } String op = evt.getActionCommand(); if (op.equals("+")) answer.setText( "x + y = " + (x+y) ); else if (op.equals("-")) answer.setText( "x - y = " + (x-y) ); else if (op.equals("*")) answer.setText( "x * y = " + (x*y) ); else if (op.equals("/")) { if (y == 0) answer.setText("Can't divide by zero!"); else answer.setText( "x / y = " + (x/y) ); } } } }

Page 35: Quest  1 define a class batsman with the following specifications

Q.20. Write a program to generate a sequence of Fibonacci Strings as follows: S0 = “a”, S1 = “b”, Sn = S(n-1) + S(n-2) where ‘+’ denotes concatenation. Thus the sequence is:a, b, ba, bab, babba, babbabab, ………. n terms and add this generated series to a file named as fibo.txt.

import java.io.*;

Page 36: Quest  1 define a class batsman with the following specifications

import java.lang.*;import java.util.*;class fibo{public static void main(String[] args){

FileOutputStream fout=null;try{

fout = new FileOutputStream("fibo.txt");char ch='y',t,ar[];String s1="a",s2="b",s3=null;t= s1.charAt(0);fout.write(t); fout.write(' '); t= s2.charAt(0); fout.write(t);fout.write(' ');Scanner reader = new Scanner(System.in);

do{

s3=s1+s2; s1=s2; s2=s3; ar=s3.toCharArray();for(int i=0;i<s3.length();i++)fout.write(ar[i]);fout.write(' ');System.out.println(" do you want to continue?(y/n)");ch = reader.next().charAt(0);}while(ch!='n');

}catch(IOException e){System.out.println(e);System.exit(-1);}finally{try{fout.close();

}catch(IOException e){}}

Page 37: Quest  1 define a class batsman with the following specifications

}}

Q21. Write a temperature conversion program using GUI either application or applet that converts from Fahrenheit to Celsius. The Fahrenheit temperature should be entered from keyboard(via textfield) and another textfield should be used to display the converted temperature. (use a proper layout for the screen)Use the following formula for the conversion.Celsius=5/9 * (Fahrenheit -32)import javax.swing.*;import java.awt.event.*;

Page 38: Quest  1 define a class batsman with the following specifications

class Converter extends JFrame{

Converter(){

super("Converter");setSize(400,400);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(null);JLabel l1=new JLabel("Enter in Fehrenheit");final JTextField t1=new JTextField(25);JButton b=new JButton("Convert To celsius");JLabel l2=new JLabel("Value in Celsius");final JTextField t2=new JTextField(25);t2.setEditable(false);l1.setBounds(20,50,200,25);t1.setBounds(200,50,200,25);b.setBounds(100,100,150,25);l2.setBounds(20,150,200,25);t2.setBounds(200,150,200,25);b.addActionListener(new ActionListener()

{ public void actionPerformed(ActionEvent e) {try {

double fahrenheit=Double.parseDouble(t1.getText());

System.out.println(fahrenheit);t2.setText(""+0.5555*(fahrenheit-32));

}catch(NumberFormatException nfe)

{ nfe.printStackTrace();}}});

add(l1);add(t1);add(l2);add(t2);add(b);

}public static void main(String a[]){

new Converter();}

}

Page 39: Quest  1 define a class batsman with the following specifications

Q.22. Create an applet which will have three radio buttons for color RED, GREEN, BLUE on selection of a radio button the background color should be change accordingly.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<APPLET CODE="choice" WIDTH=400 HEIGHT=400>

</APPLET>*/

public class choice extends Applet implements ItemListener

{

Checkbox cb1,cb2,cb3;

CheckboxGroup cbg=null;

public void init()

{

cbg=new CheckboxGroup();

Page 40: Quest  1 define a class batsman with the following specifications

cb1=new Checkbox("RED",cbg,false);

cb2=new Checkbox("GREEN",cbg,false);

cb3=new Checkbox("YELLOW",cbg,false);

add(cb1); add(cb2); add(cb3);

cb1.addItemListener(this);

cb2.addItemListener(this);

cb3.addItemListener(this);

}

public void itemStateChanged(ItemEvent evt)

{

repaint();

}

public void paint(Graphics g)

{

Checkbox c1=cbg.getSelectedCheckbox();

if(c1.getLabel()=="RED")

setBackground(Color.red);

else if( c1.getLabel() == "GREEN")

setBackground(Color.green);

else if( c1.getLabel() == "YELLOW")

setBackground(Color.yellow);

g.drawString("applet",10,100);

}

}

Page 41: Quest  1 define a class batsman with the following specifications

Output:

Q.23. Write a program to create two threads, one thread will print odd numbers and second thread will print even numbers between 1 to 10.import java.lang.*;import java.util.*;

class even extends Thread{public void run(){for(int i=1;i<=10;i++){if((i%2)==0)System.out.println("Even no. "+i);}}}class odd extends Thread{public void run(){for(int i=1;i<=10;i++){if((i%2)!=0)System.out.println("Odd no. "+i);}}}class Evenodd{public static void main(String[] args){odd od=new odd();even evn=new even();od.start();

Page 42: Quest  1 define a class batsman with the following specifications

evn.start();}}

Q24. (Loan calculator):Write an applet for calculating loan payment, as shown in below Figure. The user can enter the interest rate, the number of years, and the loan amount and click the Compute Payment button to display the monthly payment and total payment.

import java.awt.*;import java.awt.event.*;import java.applet.*;import java.lang.Math;/*<applet code="loan" width=300 height=400>

Page 43: Quest  1 define a class batsman with the following specifications

</applet>*/public class loan extends Applet implements ActionListener{TextField t1,t2,t3,t4,t5;Button b1,b2;Label l1,l2,l3,l4,l5,l6;public void init(){t1=new TextField(15);t2=new TextField(15);t3=new TextField(15);t4=new TextField(15);t5=new TextField(15);b1=new Button("Compute Payment");b2=new Button("Refresh");l1=new Label("interest rate");l2=new Label("years");l3=new Label("loan amount");l4=new Label("monthly payment");l5=new Label("total payment");l6=new Label("enter interest rate,year and loan amount");add(l6); setLayout(new FlowLayout(FlowLayout.LEFT));add(l1); setLayout(new FlowLayout(FlowLayout.RIGHT));add(t1);setLayout(new FlowLayout(FlowLayout.LEFT));add(l2);setLayout(new FlowLayout(FlowLayout.RIGHT));add(t2);setLayout(new FlowLayout(FlowLayout.LEFT));add(l3);setLayout(new FlowLayout(FlowLayout.RIGHT));add(t3);setLayout(new FlowLayout(FlowLayout.LEFT));add(l4);setLayout(new FlowLayout(FlowLayout.RIGHT));add(t4);setLayout(new FlowLayout(FlowLayout.LEFT));add(l5);setLayout(new FlowLayout(FlowLayout.RIGHT));add(t5);setLayout(new FlowLayout(FlowLayout.RIGHT));add(b1); add(b2);t1.addActionListener(this);t2.addActionListener(this);t3.addActionListener(this);t4.addActionListener(this);t5.addActionListener(this);b1.addActionListener(this);b2.addActionListener(this);

}public void actionPerformed(ActionEvent evt){if(evt.getSource() == b1){

Page 44: Quest  1 define a class batsman with the following specifications

double a=Double.parseDouble(t1.getText());double b=Double.parseDouble(t2.getText());double c=Double.parseDouble(t3.getText());double d=c*Math.pow((1+(a/100)),b);t4.setText(""+d);t5.setText(""+(c+d));

}if(evt.getSource() == b2){t1.setText(" ");t2.setText(" ");t3.setText(" ");t4.setText(" ");t5.setText(" ");}}public void paint(Graphics g){g.drawString("interest",300,300);}

}

Page 45: Quest  1 define a class batsman with the following specifications

Q25. An electronic shop has announced the following seasonal discounts on the purchase of certain items

Purchase amount Discount on Laptop Discount of Desktop PC

0-25000 0.0% 5.0%

25001-57000 5.0% 7.5%

57001-100000 7.5% 10.0%

More than 100000 10.0% 15.0%

Design a class electronic having data members as name, address, amount of purchase and the type of purchase(desktop or laptop) . Computer and print the net amountimport java.lang.*;import java.util.*;

class electronics

Page 46: Quest  1 define a class batsman with the following specifications

{String name,address,type;double amount,discount;electronics(){

name=null;address=null;type=null; amount=0.0;discount=0.0;}void compute(String str,double amt){

if(str.equals("Laptop")){

if(amt>=25001 && amt<=57000)discount=amt*(.05);

else if(amt>=57001 && amt<=100000)discount=amt*(7.5/100);

else if(amt>100000) discount=amt*(10.0/100);

}if(str.equals("Desktop PC")){

if(amt>=0 && amt<=25000) discount=amt*(5.0/100);else if(amt>=25001 && amt<=57000)

discount=amt*(7.5/100);else if(amt>=57001 && amt<=100000)

discount=amt*(10.0/100);else if(amt>100000) discount=amt*(15.0/100);

}amount -=discount;System.out.println("total amount for " + str + " is :"+amount);

}

}class elec{

public static void main(String[] args){

Scanner reader=new Scanner(System.in);electronics e=new electronics();System.out.println("enter name");e.name=reader.nextLine();System.out.println("enter address");e.address=reader.nextLine();System.out.println("1. Laptop \n2. Desktop PC\nenter choice for type");int a=reader.nextInt();switch(a){

Page 47: Quest  1 define a class batsman with the following specifications

case 1: e.type="Laptop"; break;case 2: e.type="Desktop PC"; break;default: System.out.println("entered wrong choice"); break;

}System.out.println("enter amount");e.amount=reader.nextDouble();e.compute(e.type,e.amount);

}}

Q26. Write a java program to calculate area of different shape (minimum 5 for eq. – square, rectangle….) by using method overriding concept.

import java.lang.*;import java.util.*;

class square{

double s1;square(double a){s1=a;}double area(){

return s1*s1;}

}

Page 48: Quest  1 define a class batsman with the following specifications

class rectangle extends square{

double s2;rectangle(double a,double b){super(a); s2=b;}double area(){

return s1*s2;}

}class triangle extends square{

double s2;triangle(double a,double b){super(a); s2=b;}double area(){

return s1*s2/2;}

}class circle extends square{

circle(double a){super(a);}double area(){

return 3.14*s1*s1;}

}class rhombus extends square{

double s2;rhombus(double a,double b){super(a); s2=b;}double area(){

Page 49: Quest  1 define a class batsman with the following specifications

return s1*s2/2;}

}

class area

{public static void main(String[] args)

{square s1= new square(15.0);rectangle r1= new rectangle(12.0,13.0);triangle t1= new triangle(8.0,5.0);circle c1= new circle(6.0);rhombus rm1= new rhombus(11.0,9.0);square s2;s2=s1; System.out.println("Area of Square :" +s2.area());s2=r1; System.out.println("Area of rectangle :" +s2.area());s2=t1; System.out.println("Area of triangle :" +s2.area());s2=c1; System.out.println("Area of circle :" +s2.area());s2=rm1; System.out.println("Area of rhombus :" +s2.area());

}}

Page 50: Quest  1 define a class batsman with the following specifications

Q27. Write a program that prompts the user to enter the name of an ASCII text file and display the frequency of the characters in the file.

import java.io.*;class q27{

static int temp[]=new int[255];public static void main(String[] args)

{ FileInputStream fin=null;

char c; try{

fin=new FileInputStream (args[0]);do{

c=(char)fin.read();if(c!='q') temp[c]++;else break;

} while(c!=-1);for(int i=0;i<255;i++){

if(temp[i]>0)System.out.println("char :"+(char)i+" frequency :"+temp[i]);

}

}catch (IOException e) { e.printStackTrace(); }

finally{ try{fin.close();

}catch(IOException e) {}}

}}

Page 51: Quest  1 define a class batsman with the following specifications

Q28. (Creating an investment-value calculator) Write a program that calculates the future value of an investment at a given interest rate for a specified number of years. The formula for the calculation is as follows:

futureValue = investmentAmount * (1 + monthlyInterestRate)years*12

Use text fields for interest rate, investment amount, and years. Display the future amount in a text field when the user clicks the Calculate button, as shown in Figure.