Skip to content

Commit b17a443

Browse files
committed
polymorphism
1 parent 80f3c2d commit b17a443

File tree

10 files changed

+156
-0
lines changed

10 files changed

+156
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Polymorphism2;
2+
3+
import javax.swing.*;
4+
5+
//assume that pacman eats food like cherries, bananas, strawberries
6+
//superclass is Polymorphism1.pacFood
7+
//cherries, bananas, strawberries inherit all methods and variables on food class - subclasses
8+
public class e56Polymorphism extends JFrame{
9+
public static void main(String [] args){
10+
pacman mypacman = new pacman();
11+
pacFood fo = new pacFood();
12+
pacFood so = new pacStrawnerrie();
13+
mypacman.digest(fo);
14+
mypacman.digest(so);
15+
}
16+
}

src/Polymorphism2/pacBanana.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Polymorphism2;
2+
3+
public class pacBanana extends pacFood {
4+
public void eat() {
5+
6+
System.out.println("This Banana is great");
7+
}
8+
}

src/Polymorphism2/pacCherry.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Polymorphism2;
2+
3+
public class pacCherry extends pacFood {
4+
public void eat() {
5+
6+
System.out.println("This Cherry is great");
7+
}
8+
}

src/Polymorphism2/pacFood.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Polymorphism2;
2+
3+
public class pacFood {
4+
public void eat() {
5+
6+
System.out.println("This food is great");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package Polymorphism2;
2+
3+
public class pacStrawnerrie extends pacFood {
4+
public void eat() {
5+
6+
System.out.println("This Strawberry is great");
7+
}
8+
}

src/duna8.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.awt.FlowLayout;
2+
import javax.swing.JFrame; //basic window
3+
import javax.swing.JLabel; //output text and images on the screen
4+
5+
public class duna8 extends JFrame{
6+
private JLabel item1;
7+
8+
public duna8(){
9+
super("The title bar");
10+
setLayout(new FlowLayout());
11+
item1 = new JLabel("This is a sentence");
12+
item1.setToolTipText("This is gonna show up on hover");
13+
add(item1);
14+
}
15+
}

src/duna9.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.awt.FlowLayout;
2+
import java.awt.event.ActionListener;
3+
import java.awt.event.ActionEvent;
4+
import javax.swing.JFrame; //basic window
5+
import javax.swing.JTextField;
6+
import javax.swing.JPasswordField;
7+
import javax.swing.JOptionPane;
8+
public class duna9 extends JFrame{
9+
private JTextField item1;
10+
private JTextField item2;
11+
private JTextField item3;
12+
private JPasswordField passwordField;
13+
public duna9(){
14+
super("The title");
15+
setLayout(new FlowLayout());
16+
item1 = new JTextField(10);
17+
add(item1);
18+
19+
item2 = new JTextField("Enter text here :");
20+
add(item2);
21+
22+
item3 = new JTextField("uneditable",20);
23+
item3.setEditable(false);
24+
add(item3);
25+
26+
passwordField = new JPasswordField("mypass");
27+
add(passwordField);
28+
29+
thehandler handler = new thehandler();
30+
item1.addActionListener(handler);
31+
item2.addActionListener(handler);
32+
item3.addActionListener(handler);
33+
passwordField.addActionListener(handler);
34+
}
35+
//methods is gonna be called whenever an event occurs
36+
private class thehandler implements ActionListener{
37+
public void actionPerformed(ActionEvent event){
38+
String string = "";
39+
if(event.getSource()==item1)
40+
string=String.format("field 1 : %s",event.getActionCommand());
41+
else if(event.getSource()==item2)
42+
string=String.format("field 2 : %s",event.getActionCommand());
43+
else if(event.getSource()==item3)
44+
string=String.format("field 3 : %s",event.getActionCommand());
45+
else if(event.getSource()==passwordField)
46+
string=String.format("password field is : %s",event.getActionCommand());
47+
48+
JOptionPane.showMessageDialog(null,string);
49+
}
50+
}
51+
52+
53+
}

src/e50GUI.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import javax.swing.JOptionPane;
2+
3+
public class e50GUI {
4+
public static void main(String[] args){
5+
String fn =JOptionPane.showInputDialog("Enter first number");
6+
String sn =JOptionPane.showInputDialog("Enter second number");
7+
//showInputDialog only give a string
8+
//we need to convert the variable to an integer
9+
int num1 = Integer.parseInt(fn);
10+
int num2 = Integer.parseInt(sn);
11+
//show the sum
12+
int sum = num1 + num2;
13+
//display the sum on a box
14+
JOptionPane.showMessageDialog(null,"The answer is: "+sum,"Sum",JOptionPane.PLAIN_MESSAGE);
15+
16+
17+
}
18+
}

src/e51GUIwJFrame.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import javax.swing.JFrame; //basic window
2+
3+
4+
public class e51GUIwJFrame extends JFrame{
5+
public static void main(String [] args){
6+
duna8 bucky = new duna8();
7+
bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //whenever press X program terminates
8+
bucky.setSize(278,180);
9+
bucky.setVisible(true);
10+
}
11+
}

src/e52ActionListener.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import javax.swing.JFrame;
2+
3+
4+
public class e52ActionListener extends JFrame{
5+
public static void main(String [] args){
6+
duna9 bucky = new duna9();
7+
bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
8+
bucky.setSize(350,100);
9+
bucky.setVisible(true);
10+
}
11+
}

0 commit comments

Comments
 (0)