0% found this document useful (0 votes)
72 views

Java Micro

This document contains code snippets for several Java programs: 1. A program to calculate simple interest given principal, rate, and time. 2. A program using overloading to calculate the area of a square and rectangle. 3. A program to add two matrices and display the sum. 4. A program to display powers of 2 using bitwise shift operators. 5. An interface and classes to find the sum and average of two numbers by implementing a common method.

Uploaded by

dharamshi chopda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Java Micro

This document contains code snippets for several Java programs: 1. A program to calculate simple interest given principal, rate, and time. 2. A program using overloading to calculate the area of a square and rectangle. 3. A program to add two matrices and display the sum. 4. A program to display powers of 2 using bitwise shift operators. 5. An interface and classes to find the sum and average of two numbers by implementing a common method.

Uploaded by

dharamshi chopda
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a program to evaluate simple {

interest of a given principle, rate and int


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

int m=no/100; if(i%2==1)


{
a[i]=Integer.parseInt(args[i]); System.out.println("odd "+i);
Thread.sleep(300);
System.out.println("Equivalent of if((a[i]%2)!=0)
"+args[0]+""+args[1]+" in meters is }
"+m+" meters"); }
} {

else count++; catch(InterruptedException e)


if((unit.equals("meters")) || {}
(unit.equals("m"))) }
{ throw new }
OddException(a[i]); }
class eventhread implements
int cm=no*100; Runnable
} {
} int n;
eventhread(String s)
{ Write an applet that draw a rectangle
divided in 5 equal parts.
n=Integer.parseInt(s); import java.applet.*;
} import java.awt.*;
public void run() /*
{ <applet code=rect width=400
for(int height=450>
i=0;i<=n;i++) </applet>
{ */
public class rect extends Applet
{
try public void
{ paint(Graphics g)
{
g.drawRect(100,100,200,100);
if(i%2==0) g.drawLine(100,120,300,120);
g.drawLine(100,140,300,140);
g.drawLine(100,160,300,160);
{ g.drawLine(100,180,300,180);
showStatus("Applet with
Rectangle");
}
System.out.println("even }
"+i);

Thread.sleep(300);

}
}

catch(InterruptedException e)
{}
}
}
}
Create an applet which draws a line,
rectangle and filled circle in applet display
area.
import java.applet.*;
import java.awt.*;
/*
<applet code=shapes width=400
height=450>
</applet>
*/
public class shapes extends Applet
{
public void
paint(Graphics g)
{
int x=10,
y=25;
int
width=100, height=130;

g.drawRect(x,y,width,height);

g.drawString("Rectangle",10,170);

g.drawLine(100,350,350,350);

g.drawString("Line",175,370);

g.fillOval(160,200,100,100);

g.drawString("Filled
Circle",160,330);

showStatus("Applet with
line, rectangle and filled circle");
}
}
Write an applet that draw a circle divided
in 6 equal parts.
import java.applet.*;
import java.awt.*;
/*
<applet code=divcircle width=400
height=450>
</applet>
*/
public class divcircle extends Applet
{
public void
paint(Graphics g)
{
g.drawOval(10,10,100,100);
g.drawLine(60,10,60,110;
g.drawLine(10,60,110,60;
g.drawLine(25,25,95,95);
g.drawLine(95,25,25,95);
}
}

You might also like