Java Assignment
Java Assignment
3. WAP to convert the given temperature in Fahrenheit to Celsius using the following formula
C=F-32/1.8
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.
13. WAP to calculate the area of triangle, square, circle, rectangle by using method overloading.
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?
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?
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:
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.
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.
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:
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)));
}
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:
23
Enter Breadth:
15
The area is: 345
The perimeter is: 76
The diagonal is: 27.459060435491963
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 3:-
import java.io.*;
class Temperarture
{
public static void main(String args[])throws IOException
{
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.26
Temperature 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 Cube
1 1.0 1.0
2 4.0 8.0
3 9.0 27.0
4 16.0 64.0
5 25.0 125.0
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 5:-
class QEuation
{
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());
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.: 4
Enter second no.: 6
Sum= 10
Difference= -2
Product= 24
Quotient= 0
Minimum= 4
Maximum= 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);
}
}
OUTPUT:-
Enter a no.: 56.764
Round value of num= 57.0
Ceil value of num= 57.0
Floor value of num= 56.0
Integer 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));
}
}
OUTPUT:-
The value of E= 2.718281828459045
The value of PI= 3.141592653589793
Enter a value: 2
The absolute value of 2.0: 2.0
The cosine of 2.0: -0.4161468365471424
the value of e raised to the power 2.0: 7.38905609893065
the value of natural logarithm of 2.0: 0.6931471805599453
Enter another value: 1
Greater of 2.01.0: 2.0
Smaller of 2.01.0: 1.0
The value of 2.0to the power 1.0: 2.0
Random value between 0.0 and 1.0: 0.6048349391989912
The sine of 2.0: 0.9092974268256817
The tangent of 2.0: -2.185039863261519
Square 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
{
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: 5
1
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
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);
}
}
}
OUTPUT:-
Enter limit: 9
1 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());
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: 3
Enter the base of triangle: 6
Area of triangle: 9.0
Enter the length of rectangle: 7
Enter the breadth of rectangle: 8
Area of rectangle: 56.0
Enter the side of square: 5
Area of square: 25.0
Enter the radius of circle: 6
Area 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;
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: 6
Enter the breadth of cuboid: 2
Enter the height of cuboid: 8
Volume of cuboid= 96.0
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 15:-
OUTPUT:-
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 16:-
OUTPUT:-
----------------------------------------------------------------------------------------------------------------------------------
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++;
else
countOfOdd++;
}
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();
}
System.out.println(input);
}
}
OUTPUT:-
Enter a number or q to stop:
1
Enter next number or q to stop:
2
Enter next number or q to stop:
3
Enter next number or q to stop:
4
Enter next number or q to stop:
q
Number 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
{
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: 2
Area of circle: 12.566370614359172
Circumference 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+";
else if(score<65 && score>=60)
return "C";
else if(score<55 && score>=55)
return "D+";
else if(score<50 && score>=50)
return "D";
else
return "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: 86
Your 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);
else if(sales>500 && sales<5000)
System.out.println("This commission is SR: "+0.05*sales);
else
System.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: 10000
This 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())
{
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.
This
is
my
world.
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: bhawna@gmail.com
User name: bhawna
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++;
}
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: 7
Total no. of sentences in text: 2
Total no. of e occur in text: 2
Total 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;
}
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:-
Enter name of student: bhawna
Enter quiz #1 for bhawna: 20
Enter quiz #2 for bhawna: 17
Enter quiz #3 for bhawna: 19
name: 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();
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()
{
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: 1989
Name: Sana, BirthYear: 1989, Major: Computer Science
Name: Bashir, BirthYear: 1960, Salary: 65000.0
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 28:-
class A{
int a;
float b;
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);
}
PROGRAM 29:-
OUTPUT:-
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 30:-
OUTPUT:-
----------------------------------------------------------------------------------------------------------------------------------
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
Ouputfile.txt
}
OUTPUT:-
Input
Enter Product code: 4981
Enter Product Cost: 89
Enter no. of items Available: 10
Want to insert a record(y/n): y
Enter Product code: 6937
Enter Product Cost: 100
Enter no. of items Available: 50
Want to insert a record(y/n): n
Product.txt
ProductCode ProductCost No. of items Available
4981 89 10
6937 100 50
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 33:-
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:-
grades.txt
90
89
67
56
45
83
-1
letter.txt
A+
A
C+
D+
F
B+
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));
}
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.txt
2, 9, 8
Ouputfile.txt
Roots are real and distinct.
Root1: -1.2192235935955849
Root2: -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
JOptionPane.showMessageDialog(null,"The largest no. is "+c);
}
}
OUTPUT:-
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 36(a):-
import java.awt.*;
import java.applet.Applet;
import java.awt.geom.*;
----------------------------------------------------------------------------------------------------------------------------------
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:-
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 36(C):-
import java.awt.*;
import java.applet.Applet;
import java.awt.geom.*;
}
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:-
----------------------------------------------------------------------------------------------------------------------------------
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);
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;
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;
----------------------------------------------------------------------------------------------------------------------------------
PROGRAM 40:-
import java.awt.*;
import java.awt.geom.*;
import java.applet.Applet;
----------------------------------------------------------------------------------------------------------------------------------