java assignment

54
INDEX 1. WAP to calculate sum of two numbers. 2. WAP to find the area, perimeter the diagonal of a rectangle. 3. WAP to convert the given temperature in Fahrenheit to Celsius using the following formula C=F-32/1.8 4. WAP to print the squares and cubes for the numbers 1 to 5. 5. WAP to find the roots of a quadratic equation. Assign the values of a, b and c of the equation in the program itself. 6. WAP for finding the sum, difference, product, quotient, minimum and maximum of any two integers. 7. WAP “MyNumber.java” that performs following operations on a ‘num’ variable of type double: a) Finds the round value of ‘num’ and stores the result in a variable numRound of type double. b) Finds the ceil value of ‘num’ and stores the result in a variable numCeil of type double. c) Finds the floor value of ‘num’ and stores the result in a variable numFloor of type double. d) Cast ‘num’ to type int and stores the result in a variable numInteger of type int. 8. WAP to show use of all Math class methods. 9. WAP to compute sum of the series 2+4+6+…………. +10. 10. WAP that computes the sum of the reciprocals: 1/1+1/2+1/3………. +1/10. 11. a) WAP to print Floyd’s triangle 1 2 3 4 5 6

Upload: bhawna-varshney

Post on 27-Mar-2015

1.368 views

Category:

Documents


85 download

TRANSCRIPT

Page 1: Java Assignment

INDEX

1. WAP to calculate sum of two numbers.

2. WAP to find the area, perimeter the diagonal of a rectangle.

3. WAP to convert the given temperature in Fahrenheit to Celsius using the

following formula C=F-32/1.8

4. WAP to print the squares and cubes for the numbers 1 to 5.

5. WAP to find the roots of a quadratic equation. Assign the values of a, b and

c of the equation in the program itself.

6. WAP for finding the sum, difference, product, quotient, minimum and

maximum of any two integers.

7. WAP “MyNumber.java” that performs following operations on a ‘num’ variable of type double:

a) Finds the round value of ‘num’ and stores the result in a variable numRound of type double.

b) Finds the ceil value of ‘num’ and stores the result in a variable numCeil of type double.

c) Finds the floor value of ‘num’ and stores the result in a variable numFloor of type double.

d) Cast ‘num’ to type int and stores the result in a variable numInteger of type int.

8. WAP to show use of all Math class methods.

9. WAP to compute sum of the series 2+4+6+…………. +10.

10. WAP that computes the sum of the reciprocals: 1/1+1/2+1/3……….+1/10.

11. a) WAP to print Floyd’s triangle 1

2 3

4 5 6

7 8 9 10

11 12 13 14 15

b) WAP to print Floyd’s triangle 1

0 1

1 0 1

Page 2: Java Assignment

0 1 0 1

1 0 1 0 1

12.WAP to calculate and print the first m Fibonacci numbers.

13.WAP to calculate the area of triangle, square, circle, rectangle by using

method overloading.

14.WAP to calculate the volume of cuboid by using parameterized constructor.

15.WAP that construct a Rectangle object, prints it, and then translates and

prints it three more times, so that, if rectangles were drawn, they would

form large rectangle.

16. WAP that constructs two rectangle objects, prints them, and then prints

their intersection. What happens when the rectangles do not overlap?

17.WAP involving two classes: OddAndEven and TestOddAndEven.

OddAndEven has the following:

a) instance variable countOfOdd and countOfEven both of type int

b) A method addNumber that takes a number as parameter and increment countOfOdd, if the no. is odd, else increment countOfEven.

c) A method toString that returns a string in the form :”Number of odd: x, Number of even: y”.

The TestOddAndEven class first creates OddAndEven object, then in a loop, read a number and use it to call addNumber method until the user enters Q. Finally, it prints the count of odd and even nos.

18. WAP to implement a class Circle that has methods getArea() and

getCircumference(). In constructor supply the radius of the circle.

19. WAP that constructs two rectangle objects, prints them, and then prints

their intersection. What happens when the rectangles do not overlap?

20. WAP involving class: Commission has the following:

a) Instance variable sales; an appropriate constructor;

b) A method commission() that return commission.

Page 3: Java Assignment

Now write a demo class to test the commission class by reading a sale

from the user using it to create the Commission object after validating

that value is not negative. Finally, call the commission() method to get

and print the commission.

21. WAP to read a line of text from keyboard then using StringTokenizer

class print each word of this text in separate line (one word per line) and at

last print total no. of words in the text.

22.WAP that prompts for and read e-mail address of a user. The program then

prints the user name and the domain name on different lines using

StringTokenizer class.

23.WAP to read a text from keyboard then using StringTokenizer class print

as output the following:

The no. of words in the text.

The no. of sentence in the text.

The no. of times of “e” occurs in the text.

The no. of times “z” occurs in the text.

24.Write an application TestStudent that reads a student name and use the Student class to create a student object. Then read the scores of the student in 3 quizzes and add each to the totalScore of the student of the student using addQuiz() method and print the student object.

25.WAP to design a class to represent a bank account.

26.WAP that reads in a sentence from the user and prints it out with each wortd reversed, but with the words and punctuation in the original order.

27.Implement a super class Person. Make two classes, Student and Instructor, inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write a class definitions, the constructors and the method toString() for all classes. Supply a test program that tests these classes and methods.

28.WAP where interface can be used to support multiple inheritance. Develop a standalone Java program for the example.

Page 4: Java Assignment

29.Implement the classes for the shapes using an interface for the common methods, rather than inheritance from the super class, while still Shape as a base class.

30.WAP that calls a method that throws an exception of type ArithmeticException at a random iteration in a for loop. Catch the exception in the method and pass the iteration count when the exception occurred to the calling method by using an object of an exception class you define.

31.WAP that will count the no. of character in a file.

32.WAP to create a sequential file that could store details about five products. Details include product cost, code, and no. of items available and are provided through the keyboard.

33.WAP which reads student grades from a text file called grades.txt and prints only the corresponding letter grades into a file called letter.txt.

34.WAP to read a, b, c from data file and store roots of the quadratic equation in output file. You must open your output file in append mode.

35.Develop an applet that receives three numeric values as input from the user and then displays the largest of the three on the screen.

36.Write applets to draw the following shapes: A) Cone B) Cylinder C) Square inside circle D) Circle inside square.

37.Write an applet that will display the following on a green background. Use

the following dimension:

38. Write a java applet program to plot a face.

39. Write a graphics program that draws a aclock face with a time that the

user enters in a text field.

40. Write a program that draws the picture of a house.

Rectangle: (10, 10,300,150), fill color: blueLeft Circle: (10, 10,150,150), fill color: yellowRight Circle: (159, 10,150,150), fill color: yellowText: (110, 90), fill color: red

Page 5: Java Assignment
Page 6: Java Assignment

PROGRAM 1:-

import java.io.*;class Sum{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter first no.: ");int a=Integer.parseInt(stdin.readLine());System.out.print("Enter second no.: ");int b=Integer.parseInt(stdin.readLine());System.out.println("Sum of these nos. is: "+(a+b));

}}OUTPUT:-

Enter first no.: 23Enter second no.: 34Sum of these nos. is: 57----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 2:-

import java.io.*;class Rect{

int len,br;public Rect(int leng,int bre){

len=leng;br=bre;

}void area(){

System.out.println("The area is: "+(len*br));}void perimeter(){

System.out.println("The perimeter is: "+(2*(len+br)));}

Page 7: Java Assignment

void diagonal(){

System.out.println("the diagonal is:"+Math.sqrt((Math.pow(len,2)+Math.pow(br,2)) ) );

}}public class Rectangle{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter length:");String l=stdin.readLine();int l1=Integer.parseInt(l);System.out.println("Enter Breadth:");String b=stdin.readLine();int b1=Integer.parseInt(b);Rect r=new Rect(l1,b1);r.area();r.perimeter();r.diagonal();

}}OUTPUT:-

Enter length:23Enter Breadth:15The area is: 345The perimeter is: 76The diagonal is: 27.459060435491963----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 3:-

import java.io.*;class Temperarture{

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

Page 8: Java Assignment

{BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.print("Enter temperature in Fahrenheit.: ");double f=Double.parseDouble(stdin.readLine());double c=(f-32)/1.8;System.out.println("Temperature in celsius: "+c);

}}

OUTPUT:-Enter temperature in Fahrenheit: 45.26Temperature in Celsius: 7.366666666666665----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 4:-

class Power{

public static void main(String args[]){

System.out.println("No. Square Cube");for(int i=1;i<=5;i++){

System.out.println( i+" "+Math.pow(i,2)+" "+Math.pow(i,3));

}}

}OUTPUT:-

No. Square Cube1 1.0 1.02 4.0 8.03 9.0 27.04 16.0 64.05 25.0 125.0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 5:-

class QEuation{

Page 9: Java Assignment

public static void main(String args[]){

double a=2,b=4,c=-6;double d=b*b-4*a*c;if(d<0){

System.out.println("Roots are imaginary.");}if(d==0){

System.out.println("Roots are equal.");double r1=-b/(2*a);System.out.println("Roots are: "+r1+","+r1);

}if(d>0){

System.out.println("Roots are real and distinct.");double r1=(-b+Math.sqrt(d))/(2*a);double r2=(-b-Math.sqrt(d))/(2*a);System.out.println("Roots are: "+r1+","+r2);

}}

}OUTPUT:-

Roots are real and distinct.Roots are: 1.0,-3.0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 6:-

import java.io.*;class Integers{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter first no.: ");int a=Integer.parseInt(stdin.readLine());System.out.print("Enter second no.: ");int b=Integer.parseInt(stdin.readLine());

Page 10: Java Assignment

System.out.print("Sum= "+(a+b));System.out.println("Difference= "+(a-b));System.out.println("Product= "+(a*b));System.out.println("Quotient= "+(a/b));System.out.println("Minimum= "+Math.min(a,b));

System.out.println("Maximum= "+Math.max(a,b));}

}OUTPUT:-

Enter first no.: 4Enter second no.: 6Sum= 10Difference= -2Product= 24Quotient= 0Minimum= 4Maximum= 6----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 7:-

import java.io.*;public class MyNumber{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter a no.: ");double num=Double.parseDouble(stdin.readLine());double numRound=Math.round(num);System.out.println("Round value of num= "+numRound);double numCeil=Math.ceil(num);System.out.println("Ceil value of num= "+numCeil);double numFloor=Math.floor(num);System.out.println("Floor value of num= "+numFloor);double numInteger=(int)num;System.out.println("Integer value of num= "+numInteger);

}

Page 11: Java Assignment

}OUTPUT:-

Enter a no.: 56.764Round value of num= 57.0Ceil value of num= 57.0Floor value of num= 56.0Integer value of num= 56.0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 8:-

import java.io.*;public class ma{

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

System.out.println("The value of E= "+Math.E);System.out.println("The value of PI= "+Math.PI);BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.print("Enter a value: ");double a=Double.parseDouble(stdin.readLine());System.out.println("The absolute value of "+a+": "+Math.abs(a));System.out.println("The cosine of "+a+": "+Math.cos(a));System.out.println("the value of e raised to the power "+a+":

"+Math.exp(a));System.out.println("the value of natural logarithm of "+a+":

"+Math.log(a));System.out.print("Enter another value: ");double b=Double.parseDouble(stdin.readLine());System.out.println("Greater of "+a+ b+": "+Math.max(a,b));System.out.println("Smaller of "+a+ b+": "+Math.min(a,b));System.out.println("The value of "+a+"to the power "+b+":

"+Math.pow(a,b));System.out.println("Random value between 0.0 and 1.0:

"+Math.random());System.out.println("The sine of "+a+": "+Math.sin(a));System.out.println("The tangent of "+a+": "+Math.tan(a));System.out.println("Square Root of "+a+": "+Math.sqrt(a));

}

Page 12: Java Assignment

}OUTPUT:-

The value of E= 2.718281828459045The value of PI= 3.141592653589793Enter a value: 2The absolute value of 2.0: 2.0The cosine of 2.0: -0.4161468365471424the value of e raised to the power 2.0: 7.38905609893065the value of natural logarithm of 2.0: 0.6931471805599453Enter another value: 1Greater of 2.01.0: 2.0Smaller of 2.01.0: 1.0The value of 2.0to the power 1.0: 2.0Random value between 0.0 and 1.0: 0.6048349391989912The sine of 2.0: 0.9092974268256817The tangent of 2.0: -2.185039863261519Square Root of 2.0: 1.4142135623730951----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 9:-

class Series{

public static void main(String args[]){

int sum=0;for(int i=2;i<=10;i=i+2)

sum=sum+i;System.out.println("The sum of the series: "+sum);

}}OUTPUT:-

The sum of the series: 30----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 10:-

class Series{

Page 13: Java Assignment

public static void main(String args[]){

double sum=0;for(int i=1;i<=10;i=i+1)sum=sum+1.0/i;System.out.println("The sum of the series: "+sum);

}}OUTPUT:-

The sum of the series: 2.9289682539682538----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 11(a):-

import java.io.*;class Floyd1{

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

int count,num=1;BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.print("Enter limit: ");int n=Integer.parseInt(stdin.readLine());for(int i=1;i<=n;i++){

count=0;while(count!=i){

System.out.print(num+" ");num++;count++;

}System.out.println();

}}

}OUTPUT:-

Enter limit: 51

Page 14: Java Assignment

2 3 4 5 6 7 8 9 10 11 12 13 14 15 ----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 11(b):-

import java.io.*;class Floyd2{

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

int count,num=1;BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.print("Enter limit: ");int n=Integer.parseInt(stdin.readLine());for(int i=1;i<=n;i++){

count=0;if(i%2==0)

num=0;else

num=1;while(count!=i){

System.out.print(num+" ");if(num==1)

num=0;else

num=1;count++;

}System.out.println();

}}

}OUTPUT:-

Enter limit: 7

Page 15: Java Assignment

1 0 1 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 ----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 12:-

import java.io.*;class Fibonacci{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter limit: ");int n=Integer.parseInt(stdin.readLine());int a,b,c,count=0;if(n==1)

System.out.println("1");else if(n==2)

System.out.println("1 1");else

{System.out.print("1 1 ");count=2;a=1;b=1;do{

c=a+b;a=b;b=c;System.out.print(c+" ");count++;

}while(count!=n);}

}

Page 16: Java Assignment

}OUTPUT:-

Enter limit: 91 1 2 3 5 8 13 21 34----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 13:-

import java.io.*;class Shape{

public Shape(){

}void area(double b,double h){

System.out.println("Area of triangle: "+(0.5*b*h));}void area(double a){

System.out.println("Area of square: "+(a*a));}void area(float l,float b){

System.out.println("Area of rectangle: "+(l*b));}void area(float r){

System.out.println("Area of circle: "+(Math.PI*r*r));}

}public class p13{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the height of triangle: ");double h=Double.parseDouble(stdin.readLine());

Page 17: Java Assignment

System.out.print("Enter the base of triangle: ");double b=Double.parseDouble(stdin.readLine());Shape s=new Shape();s.area(b,h);System.out.print("Enter the length of rectangle: ");float l=Float.parseFloat(stdin.readLine());System.out.print("Enter the breadth of rectangle: ");float b1=Float.parseFloat(stdin.readLine());s.area(l,b1);System.out.print("Enter the side of square: ");double a=Double.parseDouble(stdin.readLine());s.area(a);System.out.print("Enter the radius of circle: ");float r=Float.parseFloat(stdin.readLine());s.area(r);

}}OUTPUT:-

Enter the height of triangle: 3Enter the base of triangle: 6Area of triangle: 9.0Enter the length of rectangle: 7Enter the breadth of rectangle: 8Area of rectangle: 56.0Enter the side of square: 5Area of square: 25.0Enter the radius of circle: 6Area of circle: 113.09733552923255----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 14:-

import java.io.*;class Cuboid{

double l,b,h;public Cuboid(double l,double b,double h){

this.l=l;this.b=b;

Page 18: Java Assignment

this.h=h;}void volume(){

System.out.println("Volume of cuboid= "+(l*b*h));}

}public class p14{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the length of cuboid: ");double l=Double.parseDouble(stdin.readLine());System.out.print("Enter the breadth of cuboid: ");double b=Double.parseDouble(stdin.readLine());System.out.print("Enter the height of cuboid: ");double h=Double.parseDouble(stdin.readLine());Cuboid c=new Cuboid(l,b,h);c.volume();

}}OUTPUT:-

Enter the length of cuboid: 6Enter the breadth of cuboid: 2Enter the height of cuboid: 8Volume of cuboid= 96.0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 15:-

OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------PROGRAM 16:-

OUTPUT:-

Page 19: Java Assignment

----------------------------------------------------------------------------------------------------------------------------------PROGRAM 17:-

import java.io.*;class OddAndEven{

public OddAndEven(){}int n;private

int countOfOdd=0,countOfEven=0;public void addNumber(){

if(n%2==0)countOfEven++;

elsecountOfOdd++;

}public String toString(){

return ("Number of odd: "+countOfOdd+" Number of even: "+countOfEven);

}}public class TestOddAndEven{

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

OddAndEven input=new OddAndEven();BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.println("Enter a number or q to stop: ");String s=stdin.readLine();input.n= Integer.parseInt(s);while (!(s.equalsIgnoreCase("q"))){

input.n= Integer.parseInt(s);input.addNumber();System.out.println("Enter next number or q to stop: ");s=stdin.readLine();

Page 20: Java Assignment

}System.out.println(input);

}}OUTPUT:-

Enter a number or q to stop: 1Enter next number or q to stop: 2Enter next number or q to stop: 3Enter next number or q to stop: 4Enter next number or q to stop: qNumber of odd: 2 Number of even: 2----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 18:-

import java.io.*;class Circle{

double r;public Circle(double r){

this.r=r;}void getArea(){

System.out.println("Area of circle: "+Math.PI*r*r);}void getCircumference(){

System.out.println("Circumference of circle: "+2*Math.PI*r);}

}public class TestCircle{

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

Page 21: Java Assignment

{BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.print("Enter radius: ");double rad=Double.parseDouble(stdin.readLine());Circle c=new Circle(rad);c.getArea();c.getCircumference();

}}

OUTPUT:-

Enter radius: 2Area of circle: 12.566370614359172Circumference of circle: 12.566370614359172----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 19:-

import java.io.*;class Grader{

private double score;public Grader(double s){

this.score=s;}public String letterGrade(){

if(score>=90)return "A+";

else if(score<90 && score>=85)return "A";

else if(score<85 && score>=80)return "B+";

else if(score<80 && score>=75)return "B";

else if(score<75 && score>=65)return "C+";

Page 22: Java Assignment

else if(score<65 && score>=60)return "C";

else if(score<55 && score>=55)return "D+";

else if(score<50 && score>=50)return "D";

elsereturn "F";

}}public class TestGrader{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter your score: ");double sc=Double.parseDouble(stdin.readLine());Grader g=new Grader(sc);String gr=g.letterGrade();System.out.println("Your grade is: "+gr);

}}

OUTPUT:-

Enter your score: 86Your grade is: A----------------------------------------------------------------------------------------------------------------------------------PROGRAM 20:-

import java.io.*;class Commission{

public Commission(double s){

this.sales=s;}public void commission(){

if(sales<500)System.out.println("This commission is SR: "+0.02*sales);

Page 23: Java Assignment

else if(sales>500 && sales<5000)System.out.println("This commission is SR: "+0.05*sales);

elseSystem.out.println("This commission is SR: "+0.08*sales);

}}public class TestCommission{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the sales SR: ");double sc=Double.parseDouble(stdin.readLine());Commission c=new Commission(sc);c.commission();

}}OUTPUT:-

Enter the sales SR: 10000This commission is SR: 800.0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 21:-

import java.io.*;import java.util.*;public class TestString{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter Text: ");String s=stdin.readLine();StringTokenizer st=new StringTokenizer(s);int count=0;while(st.hasMoreTokens()){

Page 24: Java Assignment

String st1=st.nextToken();System.out.println(st1);count++;

}System.out.println("Total no. of words in text: "+count);

}}OUTPUT:-

Enter Text: This is my world.Thisismyworld.Total no. of words in text: 4----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 22:-

import java.io.*;import java.util.*;public class p22{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter e-mail address: ");String s=stdin.readLine();StringTokenizer st=new StringTokenizer(s);String user=st.nextToken("@");String domain=st.nextToken();System.out.println("User name: "+user);System.out.println("domain: "+domain);

}}OUTPUT:-

Enter e-mail address: [email protected] name: bhawna

Page 25: Java Assignment

domain: gmail.com----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 23:-

import java.io.*;import java.util.*;public class p23{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter Text: ");String s=stdin.readLine();StringTokenizer st=new StringTokenizer(s);int count_w=0;int count_s=0;int count_e=0;int count_z=0;while(st.hasMoreTokens()){

String st1=st.nextToken();count_w++;

} st=new StringTokenizer(s);

while(st.hasMoreTokens()){

String st1=st.nextToken(".");count_s++;

}char ch;int length=s.length();for(int i=0;i<length;i++){

ch=s.charAt(i);if(ch=='e')

count_e++;if(ch=='z')

count_z++;}

Page 26: Java Assignment

System.out.println("Total no. of words in text: "+count_w);System.out.println("Total no. of sentences in text: "+count_s);System.out.println("Total no. of e occur in text: "+count_e);System.out.println("Total no. of z occur in text: "+count_z);

}}OUTPUT:-

Enter Text: this world is mine. i like it.Total no. of words in text: 7Total no. of sentences in text: 2Total no. of e occur in text: 2Total no. of z occur in text: 0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 24:-

import java.io.*;class Student{

String name;double totalScore=0;int numberOfQuizzes=3;public Student(String name,double score){

this.name=name;this.totalScore=score;

}public Student(double score,String name){

this.totalScore=score;this.name=name;

}public Student(String name){

this.name=name;}public String getName(){

return name;}

Page 27: Java Assignment

public double getAverage(){

return (totalScore/numberOfQuizzes);}public double getTotalScore(){

return totalScore;}public void addQuiz(double score){

totalScore=score+totalScore;

}public void printStudent(){

System.out.print("name: "+name);System.out.println(" ,Average: "+this.getAverage());

}}public class TestStudent{

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

double score;BufferedReader stdin=new BufferedReader(new

InputStreamReader(System.in));System.out.print("Enter name of student: ");String n=stdin.readLine();Student s=new Student(n);for(int i=1;i<=s.numberOfQuizzes;i++){

System.out.print("Enter quiz #"+i+" for "+s.getName()+": ");score=Double.parseDouble(stdin.readLine());s.addQuiz(score);

}s.printStudent();

}}OUTPUT:-

Page 28: Java Assignment

Enter name of student: bhawnaEnter quiz #1 for bhawna: 20Enter quiz #2 for bhawna: 17Enter quiz #3 for bhawna: 19name: bhawna ,Average: 18.666666666666668

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 25:-

OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 26:-

import java.io.*;import java.util.*;public class p26{

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

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter Text: ");String s=stdin.readLine();StringTokenizer st=new StringTokenizer(s);int count=0;while(st.hasMoreTokens()){

String st1=st.nextToken();StringBuffer strbuf=new StringBuffer(st1);count=strbuf.length();if(strbuf.charAt(count-1)=='.'){

strbuf.deleteCharAt(count-1);strbuf.reverse();

Page 29: Java Assignment

System.out.print(strbuf+". ");}else{

strbuf.reverse();System.out.print(strbuf+" ");

}}

}}OUTPUT:-

Enter Text: my name is khan. I am not a terrorist.ym eman si nahk. I ma ton a tsirorret. ----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 27:-

public class p27{

public static void main(String args[]){

Person person=new Person("Bhawna",1989);Student student=new Student("Sana",1989,"Computer Science");Instructor instructor=new Instructor("Bashir",1960,65000);System.out.println(person);System.out.println(student);System.out.println(instructor);

}}class Person{

private String name;private int birthYear;public Person(String name,int birthYear){

this.name=name;this.birthYear=birthYear;

}public String toString(){

Page 30: Java Assignment

return "Name: "+name+",BirthYear: "+birthYear;}

}class Student extends Person{

private String major;public Student(String name,int birthYear,String major){

super(name,birthYear);this.major=major;

}public String toString(){

return super.toString()+",Major: "+major;}

}class Instructor extends Person{

private double salary;public Instructor(String name,int birthYear,double salary){

super(name,birthYear);this.salary=salary;

}public String toString(){

return super.toString()+",Salary: "+salary;}

}OUTPUT:-

Name: Bhawna, BirthYear: 1989Name: Sana, BirthYear: 1989, Major: Computer ScienceName: Bashir, BirthYear: 1960, Salary: 65000.0----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 28:-

class A{ int a; float b;

Page 31: Java Assignment

void Show(){ System.out.println("b in super class: " + b); }

}

class B extends A{ int a; float b;public B( int p, float q){ a = p; super.b = q; } void Show(){ super.Show(); System.out.println("b in super class: " + super.b); System.out.println("a in sub class: " + a); }

public static void main(String[] args){ B subobj = new B(1, 5); subobj.Show(); }}OUTPUT:-

b in super class: 5.0b in super class: 5.0a in sub class: 1----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 29:-

OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 30:-

OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

Page 32: Java Assignment

PROGRAM 31:-

import java.io.*;class CountCharacters{

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

File f = new File("C:/input.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); PrintWriter out=new PrintWriter(new FileWriter("outputfile.txt")); String s=br.readLine(); int count=0; while(s!=null) { count+=s.length(); s=br.readLine(); } System.out.println("no. of characters in the file =" + count); out.println("no. of characters in the file =" + count); out.close();

}}OUTPUT:-

Input.txt

My name is bhawna varshney.

Ouputfile.txt

No. of characters in the file =27----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 32:-

import java.io.*;public class p32{

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

Page 33: Java Assignment

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(new FileWriter("product.txt",true)); out.println("ProductCode ProductCost No. of items Available");

String ch; do { System.out.print("Enter Product code: "); int p_code=Integer.parseInt(stdin.readLine()); System.out.print("Enter Product Cost: "); int p_cost=Integer.parseInt(stdin.readLine()); System.out.print("Enter no. of items Available: "); int p_no=Integer.parseInt(stdin.readLine()); out.println(p_code +" "+p_cost+" "+p_no); System.out.print("Want to insert a record(y/n):"); ch=stdin.readLine(); }while (ch.equals("y"));

out.close();

}

}OUTPUT:-

InputEnter Product code: 4981Enter Product Cost: 89Enter no. of items Available: 10Want to insert a record(y/n): yEnter Product code: 6937Enter Product Cost: 100Enter no. of items Available: 50Want to insert a record(y/n): nProduct.txtProductCode ProductCost No. of items Available4981 89 106937 100 50----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 33:-

Page 34: Java Assignment

import java.io.*;import java.util.*;public class p33{

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

File f = new File("C:/grades.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); PrintWriter out=new PrintWriter(new FileWriter("letter.txt",true)); int grade; do { String s=br.readLine(); grade=Integer.parseInt(s); if(grade>=90) out.println("A+"); else if(grade<90&&grade>=85) out.println("A"); else if(grade<85&&grade>=80) out.println("B+"); else if(grade<80&&grade>=75) out.println("B"); else if(grade<75&&grade>=65) out.println("C+"); else if(grade<65&&grade>=60) out.println("C"); else if(grade<60&&grade>=55) out.println("D+"); else if(grade<55&&grade>=50) out.println("D"); else out.println("F"); }while(grade!=-1); br.close(); out.close();

}

}OUTPUT:-

Page 35: Java Assignment

grades.txt908967564583-1letter.txtA+AC+D+FB+F----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 34:-

import java.io.*;import java.util.*;public class p34{

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

File f = new File("C:/inputfile.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); PrintWriter out=new PrintWriter(new FileWriter("outputfile.txt",true)); String s=br.readLine(); StringTokenizer st=new StringTokenizer(s); double a=Double.parseDouble(st.nextToken(",")); double b=Double.parseDouble(st.nextToken(",")); double c=Double.parseDouble(st.nextToken(",")); if(b*b-4*a*c>0) { out.println("Roots are real and distinct."); out.println("Root1: "+(-b+Math.sqrt(b*b-4*a*c))/(2*a)); out.println("Root2: "+(-b-Math.sqrt(b*b-4*a*c))/(2*a));

Page 36: Java Assignment

} else if((b*b-4*a*c)==0) { out.println("Roots are equal."); out.println("Root: "+(-b)); } else { out.println("Roots are imaginary."); } br.close(); out.close();

}}OUTPUT:-

Input.txt2, 9, 8Ouputfile.txtRoots are real and distinct.Root1: -1.2192235935955849Root2: -3.2807764064044154----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 35:-

import javax.swing.JOptionPane;public class p35{

public static void main(String args[]){

String input=JOptionPane.showInputDialog("Enter three nos.");int a=Integer.parseInt(input.substring(0,2));int b=Integer.parseInt(input.substring(3,5));int c=Integer.parseInt(input.substring(6,8));if(a>b && a>c)

JOptionPane.showMessageDialog(null,"The largest no. is "+a);else if (b>a && b>c)

JOptionPane.showMessageDialog(null,"The largest no. is "+b);else

Page 37: Java Assignment

JOptionPane.showMessageDialog(null,"The largest no. is "+c);}

}OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 36(a):-

import java.awt.*;import java.applet.Applet;import java.awt.geom.*;

public class p36 extends java.applet.Applet{ public void init() { } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; Ellipse2D ellipse=new Ellipse2D.Double(15,15,90,30); g2.draw(ellipse); Ellipse2D ellipse1=new Ellipse2D.Double(15,115,90,30); g2.draw(ellipse1); Line2D line1=new Line2D.Double(15,35,15,125); g2.draw(line1);

Line2D line2=new Line2D.Double(105,30,105,130); g2.draw(line2);

Page 38: Java Assignment

}}OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 36(b):-

import java.awt.*;import java.applet.Applet;import java.awt.geom.*;public class p36 extends java.applet.Applet{ public void init() { } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; Ellipse2D ellipse=new Ellipse2D.Double(15,15,90,40); g2.draw(ellipse); Line2D line1=new Line2D.Double(15,40,60,115); g2.draw(line1); Line2D line2=new Line2D.Double(105,40,60,115); g2.draw(line2); }}OUTPUT:-

Page 39: Java Assignment

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 36(C):-

import java.awt.*;import java.applet.Applet;import java.awt.geom.*;

public class p36 extends java.applet.Applet{ public void init() { } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; Rectangle2D rec=new Rectangle2D.Double(10,10,300,300); g2.draw(rec); Ellipse2D ellipse=new Ellipse2D.Double(15,15,290,290); g2.draw(ellipse); }}OUTPUT:-

Page 40: Java Assignment

----------------------------------------------------------------------------------------------------------------------------------PROGRAM 36(d):-

import java.awt.*;import java.applet.Applet;import java.awt.geom.*;

public class p36 extends java.applet.Applet{ public void init() {

} public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; Ellipse2D ellipse=new Ellipse2D.Double(15,15,290,290); g2.draw(ellipse); Rectangle2D rec=new Rectangle2D.Double(60,60,200,200); g2.draw(rec); }}OUTPUT:-

Page 41: Java Assignment

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 37:-

import java.awt.*;import java.applet.Applet;import java.awt.geom.*;public class p37 extends java.applet.Applet{ public void init() { } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; Rectangle2D rec=new Rectangle2D.Double(10,10,300,150); g2.draw(rec); g2.setColor(Color.blue); g2.fill(rec); Ellipse2D ellipse=new Ellipse2D.Double(10,10,150,150); g2.draw(ellipse); g2.setColor(Color.yellow); g2.fill(ellipse); Ellipse2D ellipse1=new Ellipse2D.Double(159,10,150,150); g2.draw(ellipse1); g2.setColor(Color.yellow); g2.fill(ellipse1); g2.setColor(Color.red);

Page 42: Java Assignment

g2.drawString("RAMDAN MUBARAK",110,90); }}OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------PROGRAM 38:-

import java.awt.*;import java.applet.Applet;import java.awt.geom.*;public class p38 extends java.applet.Applet{ public void init() { } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; final double FACE_RADIUS=100; final double EYE_RADIUS=5; Ellipse2D.Double face=new Ellipse2D.Double(0,0,2*FACE_RADIUS,2*FACE_RADIUS); double xEye1=FACE_RADIUS/2; double yEye=FACE_RADIUS*2/3; Ellipse2D.Double eye1=new Ellipse2D.Double(xEye1-EYE_RADIUS,yEye-

EYE_RADIUS, 2*EYE_RADIUS,2*EYE_RADIUS); double xEye2=FACE_RADIUS*3/2;

Ellipse2D.Double eye2=new Ellipse2D.Double(xEye2-EYE_RADIUS,yEye-EYE_RADIUS ,2*EYE_RADIUS,2*EYE_RADIUS);

double yMouth=FACE_RADIUS*4/3;

Page 43: Java Assignment

Line2D.Double mouth=new Line2D.Double(xEye1,yMouth,xEye2,yMouth); g2.draw(face); g2.draw(eye1); g2.draw(eye2); g2.draw(mouth); }}OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 39:-

import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.applet.Applet;import javax.swing.JOptionPane;

public class p39 extends java.applet.Applet { public void init() { hours=3; minutes=0; } public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; final double RADIUS=100; final double MIN_HAND_LENGTH=90;

Page 44: Java Assignment

final double HOUR_HAND_LENGTH=80; Ellipse2D.Double circle=new Ellipse2D.Double(0,0,2*RADIUS,2*RADIUS); g2.draw(circle); final double MINUTES_PER_HOUR=60; final double MINUTES_PER_360=60*12; double minAngle=Math.PI/2-2*Math.PI*minutes/MINUTES_PER_HOUR;

Line2D.Double minHand=new Line2D.Double(RADIUS,RADIUS,RADIUS+MIN_HAND_LENGTH*Math.cos(minAngle),RADIUS-MIN_HAND_LENGTH*Math.sin(minAngle));

double hourAngle=Math.PI/2-2*Math.PI*(hours*MINUTES_PER_HOUR+minutes)

/MINUTES_PER_360; Line2D.Double hourHand=new Line2D.Double(RADIUS,RADIUS,RADIUS+HOUR_HAND_LENGTH*Math.cos(hourAngle),RADIUS-HOUR_HAND_LENGTH*Math.sin(hourAngle));

g2.draw(minHand); g2.draw(hourHand); } private int hours; private int minutes;}OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

PROGRAM 40:-

import java.awt.*;import java.awt.geom.*;import java.applet.Applet;

public class p40 extends java.applet.Applet {

Page 45: Java Assignment

public void init() { // TODO start asynchronous download of heavy resources }

public void paint(Graphics g) { Graphics2D g2=(Graphics2D)g; final double WIDTH=100; final double HEIGHT=150; double x=0; double y=200; Rectangle2D.Double front=new Rectangle2D.Double(x,y-WIDTH,WIDTH,WIDTH);

Rectangle2D.Double door=new Rectangle2D.Double(x+WIDTH/5,y-WIDTH/2, WIDTH/5 ,WIDTH/2);Rectangle2D.Double window=new Rectangle2D.Double(x+WIDTH*3/5,y-WIDTH/2 , WIDTH/5,WIDTH/5);Line2D.Double roofLeft=new Line2D.Double(x,y-WIDTH,x+WIDTH/2,y-

HEIGHT);Line2D.Double roofRight =new Line2D.Double(x+WIDTH,y-WIDTH,x+WIDTH/2,y-HEIGHT);g2.draw(front);g2.draw(door);g2.draw(window);g2.draw(roofLeft);g2.draw(roofRight);

}}OUTPUT:-

----------------------------------------------------------------------------------------------------------------------------------

Page 46: Java Assignment