/*Program Displaying the human face suing basic figures, circles, arcs, lines, etc. Done using Applets.
/* < applet code = "Face" width = 500 height =500>
< /applet> */
import java.awt.*;
import java.applet.*;
public class Face extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hi !!!!!!!!!!!!!!! I Am Ninad.",20,30);
g.drawArc(30,50,200,200,0,360); // Main Face
g.drawArc(82,80,30,30,20,160); //Left Eye Brow
g.fillArc(85,85,25,25,0,360); //Left Eye
g.drawArc(152,80,30,30,20,160); //Right Eye Brow
g.fillArc(155,85,25,25,0,360); //Right Eye
g.drawRect(90,190,80,20); //Mouth
g.drawLine(130,135,115,160); //Nose Left Line
g.drawLine(130,135,145,160); // Nose Right Line
g.drawLine(115,160,145,160); // Nose Base
g.drawString(" ------------- Created By Lionel.",10,350);
}
}
Labels: Advanced Java, Applet, Swings
class pyramid1
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
System.out.print(j+"\t");
System.out.println();
}
}
}
/* OUTPUT
1
1 2
1 2 3
Labels: Core Java
/* Java program to find percentage of student in 5 subjects
class Percentage
{
public static void main(String args[])
{
float Eng, Hindi, Marathi, Science, Social, percent;
Eng=Integer.parseInt(args[0]);
Hindi=Integer.parseInt(args[1]);
Marathi=Integer.parseInt(args[2]);
Science=Integer.parseInt(args[3]);
Social=Integer.parseInt(args[4]);
percent = (Eng+Hindi+Marathi+Science+Social)/5;
System.out.println("Subjects Offered : English, Hindi, Marathi, Science, Socail Studies");
System.out.println("Percentage"+percent);
}
}
Labels: Core Java
--BY Joel Mathias
/* All members and variables can be overriden by default in subclasses. Final Keyword Is Used in Java to prevent the subclass from overridding the members of the superclass. Making a method final ensures that the functionality defiened in the mathod will never be altered in any way. Similarily the value of the final variable can never be altered in any way. */
class fe
{
double fp;
int fr;
fe(double p1,int r1)
{
fp=p1;
fr=r1;
}
}
class se extends fe
{
double sp;
int sr;
se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp=p2;
sr=r2;
}
}
final class te extends se
{
double tp;
int tr;
te(double p1,int r1,double p2,int r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp=p3;
tr=r3;
}
double average()
{
return (fp+sp+tp)/3;
}
}
class Student
{
public static void main(String args[])
{
te student = new te(75.6,40,78.6,44,70.5,48);
System.out.println("Year\tRoll no.\tPercentage");
System.out.println("FE\t"+student.fr+"\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"\t\t"+student.tp);
System.out.println("Average performance is " + student.average());
}
}
/* Output *
Year Roll no. Percentage
FE 40 75.6
SE 44 78.6
TE 48 70.5
Average performance is 74.89999999999999 */
Labels: Core Java, Inheritance
--BY Joel Mathias
/* Program to Implement Hierarchical Inheritance in java Programming Language */
class Info
{
int pid;
char branch;
char year;
Info(int p,char ch,char y)
{
pid = p;
branch = ch;
year = y;
}
void display()
{
System.out.println("\nPID\t: "+pid);
System.out.print("Branch\t: ");
if(branch == 'i')
System.out.println("Information Technology");
if(branch =='e')
System.out.println("Electronics and Telecommunication");
if(branch =='c')
System.out.println("Computer Science");
System.out.print("Year\t: ");
if(year == 'f')
System.out.println("FE");
if(year == 's')
System.out.println("SE");
if(year == 't')
System.out.println("TE");
}
}
class Fe extends Info
{
int c;
int cpp;
Fe(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
c = m1;
cpp = m2;
}
void fdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tC\t"+c);
System.out.println("\tC++\t"+cpp);
}
}
class Se extends Info
{
int vb;
int html;
Se(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
vb = m1;
html= m2;
}
void sdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tVB\t"+vb);
System.out.println("\tHTML\t"+html);
}
}
class Te extends Info
{
int matlab;
int java;
Te(int p,char ch,char y,int m1,int m2)
{
super(p,ch,y);
matlab = m1;
java = m2;
}
void tdisplay()
{
display();
System.out.println("Performance:");
System.out.println("\tMATLAB\t"+matlab);
System.out.println("\tSJava\t"+java);
}
}
class Language
{
public static void main(String args[])
{
Fe F = new Fe(1074,'i','f',9,8);
Se S = new Se(1064,'e','s',6,8);
Te T = new Te(1054,'c','t',9,9);
F.fdisplay();
S.sdisplay();
T.tdisplay();
}
}
/* Output *
PID : 1074
Branch : Information Technology
Year : FE
Performance:
C 9
C++ 8
PID : 1064
Branch : Electronics and Telecommunication
Year : SE
Performance:
VB 6
HTML 8
PID : 1054
Branch : Computer Science
Year : TE
Performance:
MATLAB 9
SJava 9 */
Labels: Core Java, Inheritance
--BY Joel Mathias
Program that makes use of Multilevel Inheritance in java
/* program To Implement Multilevel Inheritance */
class Fe
{
double fp;
int fr;
Fe(double p1,int r1)
{
fp = p1;
fr = r1;
}
}
class Se extends Fe
{
double sp;
int sr;
Se(double p1,int r1,double p2,int r2)
{
super(p1,r1);
sp = p2;
sr = r2;
}
}
class Te extends Se
{
double tp;
int tr;
Te(double p1,int r1,double p2,int r2,double p3,int r3)
{
super(p1,r1,p2,r2);
tp = p3;
tr = r3;
}
double average()
{
return (fp+sp+tp)/3;
}
}
class Student
{
public static void main(String args[])
{
Te student = new Te(78.5,41,71.73,44,79.8,48);
System.out.println("Year\tRoll no.\tPercentage");
System.out.println("FE\t"+student.fr+"\t\t"+student.fp);
System.out.println("SE\t"+student.sr+"\t\t"+student.sp);
System.out.println("TE\t"+student.tr+"\t\t"+student.tp);
System.out.println("\nAverage Performance is " + student.average());
}
}
/* Output *
Year Roll no. Percentage
FE 41 78.5
SE 44 71.73
TE 48 79.8
Average Performance is 76.67666666666668 */
Labels: Core Java, Inheritance
--BY Joel Mathias
Single Inheritance Program To Find The Volume Of A cylinder and a sphere
/* Program To Implement Simple Program On Single Inheritance */
class Sphere
{
double r;
double area()
{
return (r * r * r * 4 * 3.1416 / 3);
}
}
class Cylinder extends Sphere
{
double h;
double area()
{
return 3.1416 * r * r * h;
}
}
class Volume
{
public static void main(String args[])
{
Cylinder C = new Cylinder();
C.r = 2.0;
C.h = 6.0;
double cyl = C.area();
Sphere S = new Sphere();
S.r = 5.0;
double sph = S.area();
System.out.println("Area of sphere is "+sph+" and area of cylinder is "+cyl);
}
}
/* OUTPUT *
Area of sphere is 523.6 and area of cylinder is 75.3984 */
Labels: Core Java, Inheritance
--BY Joel Mathias
Single Inheritance inherits the properties of one class into another class here is a program that implements single inheritance to find area of a rectangle..
/* Single Inhetitance To Find Area Of Rectangle */
class Dimensions
{
int length;
int breadth;
}
class Rectangle extends Dimensions
{
int a;
void area()
{
a = length * breadth;
}
}
class Area
{
public static void main(String args[])
{
Rectangle Rect = new Rectangle();
Rect.length = 7;
Rect.breadth = 16;
Rect.area();
System.out.println("The Area of rectangle of length "
+Rect.length+" and breadth "+Rect.breadth+" is "+Rect.a);
}
}
/* Output *
The Area of rectangle of length 7 and breadth 16 is 112 */
Labels: Core Java, Inheritance
What Is an Interface?
In general, an interface is a device or a system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks.
Within the Java programming language, an interface is a type, just as a class is a type. Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.
The bicycle class and its class hierarchy define what a bicycle can and cannot do in terms of its "bicycleness." But bicycles interact with the world on other terms. For example, a bicycle in a store could be managed by an inventory program. An inventory program doesn’t care what class of items it manages, as long as each item provides certain information, such as price and tracking number. Instead of forcing class relationships on otherwise unrelated items, the inventory program sets up a protocol of communication. This protocol comes in the form of a set of method definitions contained within an interface. The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on.
To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface. Thus, the bicycle class would provide the implementations for the methods that set and get retail price, assign a tracking number, and so on.
You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following:
- Capturing similarities among unrelated classes without artificially forcing a class relationship
- Declaring methods that one or more classes are expected to implement
- Revealing an object's programming interface without revealing its class
- Modelling multiple inheritance, a feature that some object-oriented languages support that allows a class to have more than one superclass
Simple Program On Java for the implementation of Multiple inheritance using interfaces to calculate the area of a rectangle and triangle
/* Area Of Rectangle and Triangle using Interface * /
interface Area
{
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x, float y)
{
return(x * y);
}
}
class Triangle implements Area
{
public float compute(float x,float y)
{
return(x * y/2);
}
}
class InterfaceArea
{
public static void main(String args[])
{
Rectangle rect = new Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
System.out.println("Area Of Rectangle = "+ area.compute(1,2));
area = tri;
System.out.println("Area Of Triangle = "+ area.compute(10,2));
}
}
/** OUTPUT **
Area Of Rectangle = 2.0
Area Of Triangle = 10.0
*/
Labels: Core Java, Inheritance
/* Simple Program In Java To print on the screen */
class Simple
{
public static void main(String args[])
{
System.out.println("Java Is Better Than C and C++");
}
}
/* Output *
Java Is Better Than C and C++
*/
Labels: Core Java