// 2 classes
// one uses a main method and calls the other
// very important example of class nature of objects
// and how to create them and reference them
// both classes are in the same folder
// this is first time I have showed you how to write
// your own classes and refer to them ...... study
// carefully
//-------------------class Food-----------------------------
package first_application;
import javax.swing.*;
public class Food
{
private int num;
public Food()
{
JOptionPane.showMessageDialog(null,"Create a burger
object","Constructor",JOptionPane.PLAIN_MESSAGE);
}
public void order(int number)
{
JOptionPane.showMessageDialog(null,"Preparing "+number+" burgers","ON
THE GRILL",JOptionPane.PLAIN_MESSAGE);
}
}
//-------------class CollegeStudent------------------------
package first_application;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CollegeStudent extends JFrame
{
JFrame df = new JFrame("Interaction between Student and Burger Class");
public CollegeStudent()
{
df.setSize(600,400);
df.show();
Food burger = new Food();
boolean hungry;
String res = JOptionPane.showInputDialog("Are you hungry Y / N ");
int n_bur,burger_count;
char llet[]=res.toCharArray();
if ((llet[0]=='Y') || (llet[0]=='y'))
{
n_bur=2;
burger.order(n_bur);
for(burger_count=1;burger_count<=n_bur;burger_count++)
eat_burger(burger_count);
go_to_class();
}
else
{
n_bur=4;
go_to_class();
burger.order(n_bur);
for(burger_count=1;burger_count<=n_bur;burger_count++)
eat_burger(burger_count);
}
take_nap();
draw_pictures() ;
}
public void go_to_class()
{
JOptionPane.showMessageDialog(null,"NOW IN CLASS ","
",JOptionPane.NO_OPTION);
}
public void eat_burger(int number)
{
JOptionPane.showMessageDialog(null,"EATING BURGER NUMBER
"+number,"",JOptionPane.NO_OPTION);
}
public void take_nap()
{
JOptionPane.showMessageDialog(null,"TAKING NAP","
",JOptionPane.NO_OPTION);
}
public void draw_pictures ()
{
Graphics g = df.getGraphics() ;
Font f=new Font("Serif",Font.BOLD,100);
g.setFont(f);
g.setColor(Color.blue);
g.drawString("Good Bye !!",31,230);
g.setColor(Color.red);
g.drawString("Good Bye !!",61,288);
}
public static void main(String[] args )
{
CollegeStudent student = new CollegeStudent();
student.addWindowListener(new WindowAdapter()
{
public void windowClosing ( WindowEvent e)
{
System.exit(0);
}
}
) ;
}
}
|