Color Pigment;
int Red;
int Green;
int Blue;
int X;
Red = 255;
Green = 0;
Blue = 0;
X = 0;
while (X < 255) {
Pigment = new Color (Red, Green, Blue);
g.setColor (Pigment);
g.drawLine (X, 0, X, 100);
Red = Red - 1;
Green = Green + 1;
X = X + 1;
}
while (X < 511) {
Pigment = new Color (Red, Green, Blue);
g.setColor (Pigment);
g.drawLine (X, 0, X, 100);
Green = Green - 1;
Blue = Blue + 1;
X = X + 1;
}
}
}
//create a superclass
class A
{
int a1; //public by default
protected int a2; //protected to A
void showa1a2()
{
System.out.println("a1 : "+a1+", a2 : "+a2);
}
}
//create a sunclass by extending A
class B extends A
{
int b;
void showb()
{
System.out.println("b : "+b);
}
void sum()
{
System.out.println("a1+a2+b : "+(a1+a2+b));
}
}
class SimpleInheritance2
{
public static void main(String args [])
{
//create a superclass
class A
{
private int a;
void showa()
{
System.out.println("a : "+a);
}
}
//create a sunclass by extending A
class B extends A
{
int b;
void showb()
{
System.out.println("b : "+b);
}
void sum()
{
System.out.println("a+b : "+(a+b));
}
}
class SimpleInheritance
{
public static void main(String args [])
{
A superob=new A();
B subob=new B();
//the superclass may be used by itself
superob.a=10;
System.out.println("Contents of superob : ");
superob.showa();
//the subclass has access to all public members of its superclass*/
subob.a=20;
subob.b=30;
System.out.println("Contents of subob : ");
subob.showa();
subob.showb();
System.out.println("Sum of a and b : ");
subob.sum();
}
}
/*output
Contents of superob :
a : 10
Contents of subob :
a : 20
b : 30
Sum of a and b :
a+b : 50
Press any key to continue . . .
*/
class OverloadMethod
{
public static void main(String args[])
{
Movie m=new Movie("Jaws", 120, true,"Spielberg", "PG");
m.show();
m.show("This is subclass's show method");
System.out.println(m);
}
}
/*Title : Jaws
Length : 120
Avail : true
This is subclass's show method
Director : Spielberg
Rating : PG
*/
Program to find which day of week today using Interface
interface Week
{
int Monday=1;
int Tuesday=2;
int Wednesday=3;
int Thursday=4;
int Friday=5;
int Saturday=6;
int Sunday=7;
}
class Day implements Week
{
void display(int day)
{
switch(day)
{
case Monday : System.out.println("\n Its Monday");
break;
case Tuesday : System.out.println("\n Its Tuesday");
break;
case Wednesday : System.out.println("\n Its Wednesday");
break;
case Thursday : System.out.println("\n Its Thursday");
break;
case Friday : System.out.println("\n Its Friday");
break;
case Saturday : System.out.println("\n Its Saturday");
break;
case Sunday : System.out.println("\n Its Sunday");
break;
}
}
}
class InterfaceTest3
{
public static void main(String args[])
{
Day day=new Day();
day.display(5);
}
}
Program to find which day of week today using Interface
interface Week
{
int Monday=1;
int Tuesday=2;
int Wednesday=3;
int Thursday=4;
int Friday=5;
int Saturday=6;
int Sunday=7;
}
class Day implements Week
{
void display(int day)
{
switch(day)
{
case Monday : System.out.println("\n Its Monday");
break;
case Tuesday : System.out.println("\n Its Tuesday");
break;
case Wednesday : System.out.println("\n Its Wednesday");
break;
case Thursday : System.out.println("\n Its Thursday");
break;
case Friday : System.out.println("\n Its Friday");
break;
case Saturday : System.out.println("\n Its Saturday");
break;
case Sunday : System.out.println("\n Its Sunday");
break;
}
}
}
class InterfaceTest3
{
public static void main(String args[])
{
Day day=new Day();
day.display(5);
}
}
Program to find area of rectangle and circle using Interface
interface Area //interface defined
{
float pi=3.14F;
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute (float x, float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class InterfaceTest2
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area a;
a=rect;
System.out.println("Area of rectangle : "+a.compute(5,10));
a=cir;
System.out.println("Area of circle : "+a.compute(5,0));
}
}
/*
Area of rectangle : 50.0
Area of circle : 78.5
*/
interface Area //interface defined
{
float pi=3.14F;
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute (float x, float y)
{
return(x*y);
}
void message()
{
System.out.println("Hi...this is first program based on interface");
}
}
/*class Triangle implements Area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}*/
class InterfaceTest1
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
rect.message();
System.out.println("Area of rectangle : "+rect.compute(5,10));
}
}
/*
Hi...this is first program based on interface
Area of rectangle : 50.0
*/
interface Circle extends NewShape
{
void getRadius();
int radious=10;
}
class NewCircle implements Circle
{
public void getRadius()
{
System.out.println(radious);
}
}
class ExtendInterface extends NewCircle
{
public static void main(String args[])
{
Circle nc=new NewCircle();
nc.getRadius();
}
}
//NewCircle is not abstract and does not override abstract method draw() in NewShape
//Dynamic method dispatch
class CD
{
void message()
{
System.out.println("Inside CD's method");
}
}
class Movie extends CD
{
//override message
void message()
{
System.out.println("Inside Movie's method");
}
}
class DocumentoryFilm extends CD
{
//override message
void message()
{
System.out.println("Inside DocumentoryFilm's method");
}
}
class DynamicDispatch
{
public static void main(String args[])
{
CD cd=new CD(); //object of class CD
Movie m=new Movie(); //object of class Movie
DocumentoryFilm df=new DocumentoryFilm(); //objet of class DocumentoryFilm
CD r; //obtain a reference of type CD
r=cd; // r refers to CD object
r.message();
r=m; // r refers to Movie object
r.message();
r=df; // r refers to DocumentoryFilm object
r.message();
}
}
//super class
class Person
{
protected String name;
protected int code;
Person(String n,int c)
{
name=n;
code=c;
}
void put_namecoade( )
{
System.out.println("\n Name of person is: "+name);
System.out.println("\n Code is: "+code);
}
}
//intermediate super class
class Account extends Person
{
protected int pay;
class CDStore1
{
public static void main(String args[])
{
Movie m1=new Movie("Jaws", 120, true,"Spielberg", "PG");
Movie m2=new Movie();
Movie m3=new Movie(m1);
class WriteDemo
{
public static void main(String args[ ])
{
int i; char c ; float f;double d;
i = 'J';
c = 'A';
f = 'V';
d = 'A';
System.out.write(i);
System.out.write(c);
System.out.write((int)f); // convertion from float to integer
System.out.write((int)d); // convertion from double to integer
System.out.write('\n');
}
}
/* Program to read a string and rewrite it in alphabetical order */
import java.io.DataInputStream; // to load DataInputStream class
class P27
{
public static void main(String args[ ])
{
String str=new String();
char c[] = new char[15];
char temp;
int len=0;
DataInputStream in = new DataInputStream(System.in);
7. Which is the valid declarations within an interface definition?
A. public double methoda();
B. public final double methoda();
C. static void methoda(double d1);
D. protected void methoda(double d1); Click for answer
A. public double methoda();
8. Which one is a valid declaration of a boolean?
A. boolean b1 = 0;
B. boolean b2 = 'false';
C. boolean b3 = false;
D. boolean b4 = Boolean.false();
E. boolean b5 = no; Click for answer
import java.io.DataInputStream; // load class for reading purpose
class StringtoNumber
{
public static void main(String args[])
{
DataInputStream in = new DataInputStream(System.in); // creating object of class DataInputStream.
int intNumber =0;
float floatNumber = 0.0F;
try
{
System.out.print("Enter an integer Number :");
intNumber = Integer.parseInt(in.readLine());
System.out.print("Enter a float Number :");
floatNumber = Float.valueOf(in.readLine()).floatValue();
}
catch(Exception e) { }
System.out.println("Integer Number is : "+intNumber);
System.out.println("Float Number is : "+floatNumber);
}
}
class ReturnObject
{
public static void main(String args[])
{
Rational r1 = new Rational(2,5);
Rational r2 = new Rational(3,7);
Rational r3 = new Rational();
r3 = r1.sum(r2); // Objects passed as arguments & return type of called function is object
/* Program to demonsrates Recursion - Factorial of a given number */
class Factorial
{
int fact(int n)
{
int result;
if(n==1)
return 1;
result=fact(n-1)*n; // recursive call to fact() function
return result;
}
}
class RecursionDemo
{
public static void main(String args[])
{
Factorial f = new Factorial();
System.out.println(" Factorial of 5 is : "+f.fact(5));
System.out.println(" Factorial of 6 is : "+f.fact(6));
System.out.println(" Factorial of 7 is : "+f.fact(7));
System.out.println(" Factorial of 8 is : "+f.fact(8));
}
}
/* Program to demonstrate ‘Using Object as Parameters’ */
class Rectangle
{
int length;
int width;
// Construct clone of an object
Rectangle(Rectangle obj)
{
length = obj.length;
width = obj.width;
}
// Constructor used when all values are specified
Rectangle(int l,int w)
{
length = l;
width = w;
}
// Constructor used when no values are specified
Rectangle()
{
length = 0;
width = 0;
}
// Constructor used when one value is specified for both,length and width
Rectangle(int l)
{
length = width = l;
}
// compute and return Area of a Rectangle
int area()
{
return length*width;
}
}
class RectangleDemo9
{
public static void main(String args[])
{
// create Rectangles using the various constructors
Rectangle rect1 = new Rectangle(20,15);
Rectangle rect2 = new Rectangle();
Rectangle rect3 = new Rectangle(20);
int a;
Rectangle rect4 = new Rectangle(rect1);
// get area of first rectangle
a = rect1.area();
System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
// get area of second rectangle
a= rect2.area();
System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);
// get area of Third rectangle
a= rect3.area();
System.out.println("Area of Third Rectangle(length:"+rect3.length+", width:"+rect3.width+") is: "+a);
// get area of clone
a= rect4.area();
System.out.println("Area of Clone(length:"+rect4.length+", width:"+rect4.width+") is: "+a);
}
}
// use 'this' to resolve name-space collisions.
Rectangle(int length ,int width)
{
this.length = length ;
this.width = width;
}
// compute and return Area of a Rectangle
int area()
{
return length*width;
}
}
class RectangleDemo8
{
public static void main(String args[])
{
// create Rectangles
Rectangle rect1 = new Rectangle(20,15);
Rectangle rect2 = new Rectangle(25,17);
int a;
// get area of first rectangle
a = rect1.area();
System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
// get area of second rectangle
a= rect2.area();
System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);
/* Program to demonstrate Constructor overloading */
class Rectangle
{
int length;
int width;
// Constructor used when all values are specified
Rectangle(int l,int w)
{
length = l;
width = w;
}
// Constructor used when no values are specified
Rectangle()
{
length = 0;
width = 0;
}
// Constructor used when one value is specified for both,length and width
Rectangle(int l)
{
length = width = l;
}
// compute and return Area of a Rectangle
int area()
{
return length*width;
}
}
class RectangleDemo7
{
public static void main(String args[])
{
// create Rectangles using the various constructors
Rectangle rect1 = new Rectangle(20,15);
Rectangle rect2 = new Rectangle();
Rectangle rect3 = new Rectangle(20);
int a;
// get area of first rectangle
a = rect1.area();
System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
// get area of second rectangle
a= rect2.area();
System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);
// get area of Third rectangle
a= rect3.area();
System.out.println("Area of Third Rectangle(length:"+rect3.length+", width:"+rect3.width+") is: "+a);
}
}
/* Program to demonstrate parametrized Constructor */
class Rectangle
{
int length;
int width;
// This is parameterized Constructor for Rectangle
Rectangle(int l,int w)
{
length = l;
width = w;
}
// compute and return Area of a Rectangle
int area()
{
return length*width;
}
}
class RectangleDemo6
{
public static void main(String args[])
{
// Declare, allocate, and initialize Rectangle Object
Rectangle rect1 = new Rectangle(20,15);
Rectangle rect2 = new Rectangle(25,17);
int a;
// get area of first rectangle
a = rect1.area();
System.out.println("Area of First Rectangle(length:"+rect1.length+", width:"+rect1.width+") is: "+a);
// get area of second rectangle
a= rect2.area();
System.out.println("Area of Second Rectangle(length:"+rect2.length+", width:"+rect2.width+") is: "+a);
}
}
// This is Constructor for Rectangle
Rectangle()
{
System.out.println(" Constructing Rectangle....");
length = 20;
width = 15;
}
// compute and return Area of a Rectangle
int area()
{
return length*width;
}
}
class RectangleDemo5
{
public static void main(String args[])
{
// Declare, allocate, and initialize Rectangle Object
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
int a;
// get area of first rectangle
a = rect1.area();
System.out.println(" Area of Rectangle is : "+a);
// get area of second rectangle
a= rect2.area();
System.out.println(" Area of Rectangle is : "+a);
}
}
// compute and return Area of a Rectangle
int area()
{
return length*width;
}
}
class RectangleDemo3
{
public static void main(String args[])
{
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
int a;
//assign values to rect1's instance variables
rect1.length= 20;
rect1.width=15;
//assign different values to rect2's instance variables
rect2.length= 25;
rect2.width=17;
System.out.print(" First Recangle (length : "+rect1.length+" , width : "+rect1.width+") --->");
a = rect1.area(); // get area of first rectangle
System.out.println(" Area is : "+a);
System.out.print(" Second Recangle (length : "+rect2.length+" , width : "+rect2.width+") --->");
a= rect2.area(); // get area of second rectangle
System.out.println(" Area is : "+a);
}
}
/* Program to demonstrate the use of other class.
Call this file RectangleDemo1.java */
class Rectangle
{
int length;
int width;
}
// This class declares an object of type Rectangle.
class RectangleDemo1
{
public static void main(String args[])
{
Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
int area;
//assign values to rect1's instance variables
rect1.length= 20;
rect1.width=15;
//assign different values to rect2's instance variables
rect2.length= 25;
rect2.width=17;
// compute area of first rectangle
area= rect1.length* rect1.width;
System.out.println(" Area of Rectangle (length : "+rect1.length+" , width : "+rect1.width+") is : "+area);
// compute area of second rectangle
area= rect2.length* rect2.width;
System.out.println(" Area of Rectangle (length : "+rect2.length+" , width : "+rect2.width+") is : "+area);
}
}
Program to compute value of x1 and x2 of linear equations
import java.io.DataInputStream; // to load DataInputStream class
class LinearEq
{
public static void main(String args[])
{
int a=0,b=0,c=0,d=0,m=0,n=0,deno=0;
double x1=0.0,x2=0.0;
DataInputStream in = new DataInputStream(System.in);
System.out.println(" Two Linear Equations are :");
System.out.println("\t ax1 + bx2 = m ");
System.out.println("\t cx1 + dx2 = n \n");
try
{
System.out.print("Enter a : ");
a= Integer.parseInt(in.readLine());
System.out.print("Enter b : ");
b = Integer.parseInt(in.readLine());
System.out.print("Enter c : ");
c = Integer.parseInt(in.readLine());
System.out.print("Enter d : ");
d = Integer.parseInt(in.readLine());
System.out.print("Enter m : ");
m = Integer.parseInt(in.readLine());
System.out.print("Enter n : ");
n = Integer.parseInt(in.readLine());
}
catch(Exception e) { System.out.println("I/O Error"); }
// To display the name and balance
void show()
{
System.out.println("\nDepositor name :"+name);
System.out.println("Type of Account :"+type);
System.out.println("Balance :"+bal);
}
}
class P25
{
public static void main(String args[ ])
{
int choice;
int ans;
Bank b1=new Bank("Stavan","Saving",50000); //providing initial value by constructor
b1.show();
//creating object of class DataInputStream.
DataInputStream in = new DataInputStream(System.in);
try
{
do
{
System.out.println("\n1. Deposit");
System.out.println("2. Withdraw");
System.out.print("Enter your choice (1/2) :");
choice = Integer.parseInt(in.readLine());
if(choice ==1)
b1.deposit();
if(choice ==2)
b1.withdraw();
System.out.println("\nDo you want to continue ?(1: yes /0 : No)");
ans = Integer.parseInt(in.readLine());
}while(ans==1);
}
catch(Exception e) { System.out.println("I/O Error"); }
import java.io.DataInputStream; // to load DataInputStream class
class Bank
{
String name,type;
int acno,bal,wit,dep;
// To assign initial values
void getdata()
{
//creating object of class DataInputStream.
DataInputStream in = new DataInputStream(System.in);
try
{
System.out.print(" Enter Name of Account Holder : ");
name=in.readLine();
System.out.print(" Enter Type of Account : ");
type=in.readLine();
System.out.print(" Enter initial amount in Account : ");
bal = Integer.parseInt(in.readLine());
}
catch(Exception e) { System.out.println("I/O Error"); }
}
// To deposit an amount
void deposit()
{
//creating object of class DataInputStream.
DataInputStream in = new DataInputStream(System.in);
try
{
System.out.print(" Enter amt to deposit : ");
dep = Integer.parseInt(in.readLine());
// To display the name and balance
void show()
{
System.out.println("\nDepositor name :"+name);
System.out.println("Type of Account :"+type);
System.out.println("Balance :"+bal);
}
}
class P24
{
public static void main(String args[ ])
{
int choice;
int ans;
Bank b1=new Bank();
b1.getdata();
//creating object of class DataInputStream.
DataInputStream in = new DataInputStream(System.in);
try
{
do
{
System.out.println("\n1. Deposit");
System.out.println("2. Withdraw");
System.out.print("Enter your choice (1/2) :");
choice = Integer.parseInt(in.readLine());
if(choice ==1)
b1.deposit();
else
b1.withdraw();
System.out.println("\nDo you want to continue ?(1: yes /0 : No)");
ans = Integer.parseInt(in.readLine());
}while(ans==1);
}
catch(Exception e) { System.out.println("I/O Error"); }