You are on page 1of 4

Write a program to evaluate simple class Overload Math.

pow( 1+(rate/number),
interest of a given principle, rate and { number*time);
time. System.out.println("Price is double area(float
public class Main 1500.00 Rupees"); x) cinterest = amount - principle; System.out.println("Sum of 3x3
{ { System.out.println("Compound matrices\n");
public static void main (String args[]) return interest = "+ cinterest);
{ float p, r, t, si; price=0.04*1500.00+1500.00; x*x; System.out.println("Total amount =
p = 13000; r = 12; t = 2; } "+ amount); for(i=0;i<m;i++)
si = (p*r*t)/100; double area(float } {
System.out.println("Simple Interest is: " x, float y) }
+si); System.out.println("After {  Write a program to find sum
}} adding sales tax, Price return of two matrices of 3x3.
A car accessories shop assigns code 1 is:"+price); x*y; for(j=0;j<n;j++)
break; import java.util.Scanner;
to seat covers, 2 to steering wheel } class AddMatrix
covers, 3 to car lighting and 4 to air } {
purifiers. All other items have code 5 or case 4: class Area public static void
more. While selling goods, a sales tax of { main(String args[])
2% to seat covers, 3% to steering wheel public static void { System.out.print(sum[i][j]+"\t");
covers, 4% to car lighting, 2.5% to air System.out.println("Air main(String args[]) int
purifiers and 1.2% to other items is Purifiers"); { m=3,n=3,i,j;
charged. A list containing the product int
code and price is given for making a bill. x,y; System.out.println();
Write a java program using switch System.out.println("Price is Scanner in=new }
statements to prepare a bill. 2000.00 Rupees"); Scanner(System.in); }
import java.util.*; int }
public class car { x=Integer.parseInt(args[0]); matrix1[][]=new int[m][n];  Write an interface called
public static void main (String int numbers, with a method int
[] args) price=0.025*2000.00+2000.00;
matrix2[][]=new int[m][n]; Process(int x,int y). Write a
{ int class called Sum, in which
Scanner pc= new y=Integer.parseInt(args[1]); sum[][]=new int[m][n]; the method Process finds
Scanner(System.in); System.out.println("After Overl the sum of two numbers and
System.out.println("Enter the adding sales tax, Price oad obj=new returns an int value. Write
product code"); is:"+price); Overload(); another class called
int code= pc.nextInt(); break; Average, in which the
default : System.out.println("Enter
elements for first 3x3 matrix\ Process method finds the
n"); average of the two numbers
double price; System.out.println("Area of and returns an int.
switch(code) { System.out.println("Other"); Square is: "+obj.area(x)); interface numbers
case 1 : Syste {
for(i=0;i<m;i++) public int
m.out.println("Are {
System.out.println("Price is a of Rectangle is: Process(int x,int y);
System.out.println("Seat 1200.00 Rupees"); "+obj.area(x,y)); }
covers"); } } class Sum implements
for(j=0;j<n;j++) numbers
price=0.012*1200.00+1200.00; {
 Write a program to display public int
System.out.println("Price is powers of 2 i.e. 2, 4, 8, 16
1000.00 Rupees"); Process(int x,int y)
etc. up to 1024 using Shift matrix1[i] {
System.out.println("After operator. [j]=in.nextInt(); return
adding sales tax, Price class power } x+y;
price=0.02*1000.00+1000.00; is:"+price); { }
public static void }
} main(String args[]) class Average implements
System.out.println("After } { numbers
adding sales tax, Price int System.out.println("Enter
elements for second 3x3 {
is:"+price); a=1; public int
 Write a program to scan 3 for(int matrix\n");
break; integer values from Process(int x, int y)
i=1;i<=10);i++) {
command line arguments {
case 2: and display the maximum return
using conditional operator. for(i=0;i<m;i++) (x+y)/2;
a=a<<1; { }
public class Max
System.out.println("Steering { }
wheel covers"); public static void main (String class Interface
args[]) System.out.println(a); for(j=0;j<n;j++) {
{ } public static void
int a, b, c, max; } } main(String args[])
System.out.println("Price is  A bank gives 6.5% per {
500.00 Rupees"); annum interest on deposits int
matrix2[i]
made in that bank. Write a [j]=in.nextInt(); a,b;
a=Integer.parseInt(args[0]); program to calculate the Sum
b=Integer.parseInt }
price=0.02*500.00+500.00; total amount that a person add=new Sum();
(args[1]); will receive after the end of 5
c=Integer.parseInt years for a deposit of Rs.
(args[2]); 5000 for compound interest. for(i=0;i<m;i++)
System.out.println("After max= (a > b) ? (a {
adding sales tax, Price > c ? a : c) : (b > c ? b : c); public class CI a=add.Process(10,20);
is:"+price); {
break; public static void main(String[] args) {
for(j=0;j<n;j++)
System.out.println(“Maximum double principle = 5000, rate = 6.5,
case 3: is”+max); System.out.println("Sum is=\
}} time = 5; t"+a);
int number = 1; sum[i]
 Write a program to calculate double amount,cinterest; [j]=matrix1[i][j]+matrix2[i][j];
System.out.println("Car the area of square and }
lighting"); amount = principal * Average avg=new
rectangle by overloading
area method. Average();
class Cabbage extends lowercase
Vegetable characters in it.
{
b=avg.Process(10,20); public String System.out.println(p); class cmd System.out.println(args[0]+" is
toString() { not palindrome");
{ public static void
main(String args[])
System.out.println("Average System.out.println(b); {
is=\t"+b); color="Green } char System.out.println("\n4. Total
} color"; } c[]=args[0].toCharArray(); number of uppercase and
} return int i; lowercase characters in it");
"Cabbage=>" +color; int
}  Create package pack1 within
 Declare an abstract class this package create class A upper=0,lower=0;
}
Vehicle with an abstract class Carrot extends which contains one instance
method named numWheels() variable and one instance System.out.println("\n1.
Vegetable Display each character on
provide subclasses Car and { method. Create another
Truck that each implements package pack2 within this separate line in reverse for(i=0;i<c.length;i++)
public String order");
this method. Create instance toString() package create class B {
of these subclasses and { where class B is calling the
demonstrate the use of this method and variable of class
method. A. for(i=c.length-
1;i>=0;i--) if(Character.isUpperCase(c[i]))
abstract class Vehicle color="Red color";
return package pack1;
{ public class A
public abstract "Carrot=>" +color;
} {
void numWheels(); public int a=10; System.out.println(c[i]); upper++;
} }
class Potato extends public void
class Car extends Vehicle printA()
{ Vegetable
{ { else
public void System.out.println("\n2. Count
numWheels() public String
toString() total no. of characters and
{ display each character's
{
System.out.println("Package position too"); lower++;
pack1, class A and method }
color="Brown printA");
System.out.println("Car has 4 }
wheels"); color";
return } System.out.println("\nTotal
} number of characters are
} "Potato=>" +color; System.out.println("\nNo. of
} package pack2; "+c.length); uppercase characters=
class Truck extends Vehicle import pack1.*;
{ } "+upper);
public void class Brinjal extends class B
numWheels() Vegetable {
{ { public static void for(i=0;i<c.length;i++)
public String main(String args[])
toString() { System.out.println("\nNo. of
{ A lowercase characters=
a1=new A(); "+lower);
System.out.println("Truck has System.out.println(c[i]+" }
6 wheels"); character is at "+i+" position"); }
} color="Purple
} color";
return System.out.println("Instance  Write a program that takes a
class Demo Variable "+a1.a); string from the user and
{ "Brinjal=>" +color; System.out.println("\n3.
} validate it. The string should
public static void Identify the string is be at least 5 characters and
main(String args[]) } palindrome or not");
class Col should contain at least one
{ String digit. Display an appropriate
Car { System.out.println("Instance s1=args[0];
public static void method calling"); valid message.
c=new Car();
main(String args[])
{ class Val
StringBuffer {
c.numWheels(); a1.printA(); buffer=new StringBuffer(s1);
} public static void
Truck main(String args[])
t=new Truck(); Cabbage c=new }
Cabbage();  Write a program that accepts {
buffer.reverse(); String
a String from command line String s=args[0];
t.numWheels(); and perform the following s2=buffer.toString(); char
} Carrot ca=new operations: c[]=s.toCharArray();
} Carrot(); o Display each int
 The abstract Vegetable class character on flag=0,i;
has four subclasses named separate line in
reverse order System.out.println(s1.equals(s
Cabbage, Carrot, Brinjal and Potato p=new 2));
Potato. Declare one instance Potato(); o Count total
number of if(s.length()>=5)
variable of type string that {
indicates the color of characters and
vegetable. Create and Brinjal b=new display each
display instances of these Brinjal(); character’s
object. Override the position too. if(s1.equals(s2))
toString() method of object o Identify that for(i=0;i<s.length();i++)
to return a string with the whether the
name of the vegetable and string is
System.out.println(c); palindrome or {
its color. System.out.println(args[0]+" is
abstract class Vegetable not palindrome");
{ o Count total else
public String color; number of
} System.out.println(ca); uppercase and
Exception: unit is not valid
if(Character.isDigit(c[i])) "+s); public class oddeventhread
} {
} System.out.println("Number public static void
{ class Convert Format Exception occur"); a[i]=Integer.parseInt(args[i]); main(String args[])
{ } {
public static void String
main(String args[]) if((a[i] s=args[0];
flag=1; { %2)!=0)
int catch(ArithmeticException e)
no; Runnable r1=new
String {
unit; { oddthread(s);
try
System.out.println("\nString is {
valid"); System.out.println("Arithemtic Thread t1=new
Exception occur"); count++; Thread(r1);
}
no=Integer.parseInt(args[0]);
break; Runnable r2=new
throw new eventhread(s);
unit=args[1]; catch(ArrayIndexOutOfBounds OddException(a[i]);
} Exception e)
{ Thread t2=new
} Thread(r2);
} if((unit.equals("centimeter")) ||
(unit.equals("cm"))) System.out.println("Array } t1.start();
if(flag==0) Index Out Of Bounds
Exception occur");
{ } t2.start();
catch(ArithmeticException e) try
int {
System.out.println("String is catch(Exception
m=no/100; e)
not valid\nIt should contain at { {
least 1 digit."); t1.join();
}
else
System.out.println("Equivalent System.out.println("Exception t2.join();
of "+args[0]+""+args[1]+" in "+e.getMessage()); System.out.println("Arithemtic }
meters is "+m+" meters"); } Exception occur");
System.out.println("String is }
not valid\nIt should be at least }
5 characters."); } } catch(InterruptedException e)
}  Write a program that accepts {
} 5 even numbers from
else command line, if any of the catch(Exception
 Write an application that if((unit.equals("meters")) || numbers is odd then throw e)
converts between meters (unit.equals("m"))) custom exception System.out.println(e);
and feet. Its first command- OddException and count }
line argument is a number such invalid numbers.
and second command line {
argument is either {
“centimetre” or”meter”. If class OddException extends
Exception System.out.println("End of
the argument equals { Main Thread: Both threads
“centimetre” displays a int completed");
string reporting the cm=no*100; OddException(int System.out.println("Exception:
n) }
equivalent number of { no. should be even"); }
meters. If this argument class oddthread implements
equals “meters”, display a Runnable
string reporting the } {
equivalent number of System.out.println("Equivalent } int n;
centimetre. If unit is not of "+args[0]+""+args[1]+"in System.out.println(n+" is
Odd"); oddthread(String
given properly then generate centimeters is "+cm+" s)
custom exception centimeters"); }
} {
Unitformatexception. If first System.out.println("Invalid
argument is not proper class Even
{ numbers= "+count);
format then generate } }
numberformatexception. public static void
main(String args[]) } n=Integer.parseInt(s);
Generate other exception as
per requirements. (1 { }
meter=100 centimetre) else int  Write an application that public void run()
a[]=new int[5]; starts two threads. First {
int thread displays even for(int
class UnitformatException count=0; numbers in the range i=0;i<=n;i++)
extends Exception throw
new UnitformatException(unit); for(int specified from the command {
{ i=0;i<5;i++) line and second thread
}
{ displays odd numbers in the
same range. Each thread try
UnitformatException(String s) waits for 300 milliseconds
{ try before displaying the next
catch(NumberFormatExceptio
n e) numbers. The application {
{ waits for both the thread to
{ finish and then displays the
System.out.println("Unitformat message “Both threads
completed”.
if(i
%2==1) g.drawLine(60,10,60,110);
{}
}
{ }
} g.drawLine(10,60,110,60);

 Create an applet which


draws a line, rectangle and
System.out.println("odd "+i); filled circle in applet display g.drawLine(25,25,95,95);
area.
import java.applet.*;
import java.awt.*;
/* g.drawLine(95,25,25,95);
Thread.sleep(300); <applet code=shapes }
width=400 height=450> }
</applet>
} */  Write an applet that draw a
public class shapes extends rectangle divided in 5 equal
Applet
{ parts.
} public void import java.applet.*;
paint(Graphics g) import java.awt.*;
{ /*
int <applet code=rect width=400
catch(InterruptedException e) x=10, y=25; height=450>
int </applet>
width=100, height=130; */
public class rect extends
{} Applet
} {
} public void
} g.drawRect(x,y,width,height); paint(Graphics g)
class eventhread implements {
Runnable
{
int n; g.drawString("Rectangle",10,1
eventhread(String 70); g.drawRect(100,100,200,100);
s)
{

g.drawLine(100,350,350,350); g.drawLine(100,120,300,120);
n=Integer.parseInt(s);
}
public void run() g.drawString("Line",175,370);
{ g.drawLine(100,140,300,140);
for(int
i=0;i<=n;i++)
{
g.fillOval(160,200,100,100); g.drawLine(100,160,300,160);

try
g.drawString("Filled g.drawLine(100,180,300,180);
{ Circle",160,330);

if(i showStatus("Applet with


%2==0) showStatus("Applet with line, Rectangle");
rectangle and filled circle"); }
} }
}
{  Write an applet that draw a
circle divided in 6 equal
parts.
import java.applet.*;
import java.awt.*;
System.out.println("even "+i); /*
<applet code=divcircle
width=400 height=450>
</applet>
*/
Thread.sleep(300); public class divcircle extends
Applet
{
} public void
paint(Graphics g)
{
}

g.drawOval(10,10,100,100);
catch(InterruptedException e)

You might also like