41. Create a program that accepts channel network frequency signal either of 2, 4, 5, 21, and 27. When 2 is entered, display “TV5”. “ABS-CBN”, “GMA”, “Studio 23”, and “QTV” respectively. None specified number aforementioned is invalid. Use JCombobox or JListbox in your selection.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChannelGUI extends JFrame{
private JButton btnOK,btnCANCEL;
private JLabel jl1,jlResult;
private JComboBox cbo1;
private ButtonHandlerOK btnok;
private ButtonHandlerCANCEL btncancel;
public ChannelGUI(){
jl1=new JLabel("Please select channel: ");
jlResult=new JLabel();
cbo1=new JComboBox();
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
btnok=new ButtonHandlerOK();
btnOK.addActionListener(btnok);
btncancel=new ButtonHandlerCANCEL();
btnCANCEL.addActionListener(btncancel);
cbo1.addItem("2");
cbo1.addItem("4");
cbo1.addItem("5");
cbo1.addItem("21");
cbo1.addItem("27");
Container pane= new Container();
pane.setLayout(new GridLayout(3,2));
pane.add(jl1);
pane.add(cbo1);
pane.add(btnOK);
pane.add(btnCANCEL);
pane.add(jlResult);
add(pane);
setTitle("Amante Inc.");
setSize(500,180);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public class ButtonHandlerOK implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
String input="";
input=(String)cbo1.getSelectedItem();;
if(input.equals("2")){
jlResult.setText("Network: TV5");
}
else if(input.equals("4")){
jlResult.setText("Network: ABS-CBN");
}
else if(input.equals("5")){
jlResult.setText("Network: GMA");
}
else if(input.equals("21")){
jlResult.setText("Network: Studio 23");
}
else if(input.equals("27")){
jlResult.setText("Network: QTV");
}
}
}
public class ButtonHandlerCANCEL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Program terminating...");
System.exit(0);
}
}
public static void main(String[] args) {
new ChannelGUI();
}
}
OBJECT-ORIENTED PROGRAMMING CS211L
Lunes, Setyembre 30, 2013
LabExer#40
40. Consider the GUI below, determine if the number is either of the following:
ODD-POSITIVE
ODD-NEGATIVE
EVEN-POSITIVE
EVEN-NEGATIVE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberGUI extends JFrame{
private static String str="";
private JLabel jl1,jlResult;
private JTextField jt1;
private JButton btnOK,btnCANCEL;
private ButtonHandlerOK btnok;
private ButtonHandlerCANCEL btncancel;
public NumberGUI(){
jl1=new JLabel("Enter a number: ");
jlResult=new JLabel();
jt1=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
btnok=new ButtonHandlerOK();
btnOK.addActionListener(btnok);
btncancel=new ButtonHandlerCANCEL();
btnCANCEL.addActionListener(btncancel);
Container pane= new Container();
pane.setLayout(new GridLayout(3,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(btnCANCEL);
pane.add(jlResult);
add(pane);
setTitle("Amante Inc.");
setSize(500,180);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public class ButtonHandlerOK implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int num=0;
num=Integer.parseInt(jt1.getText());
String display=ODDEVEN(num);
jlResult.setText(num + " is "+display);
}
public String ODDEVEN(int num){
if(num==0){
str="";
}
else if(num%2==0){
str="EVEN";
}
else {
str="ODD";
}
NEGAPOS(num);
return str;
}
public String NEGAPOS(int num){
if(num<0){
str=str+"-NEGATIVE";
}
else if(num>0){
str=str+"-POSITIVE";
}
else{
str=str+"ZERO";
}
return str;
}
public boolean isEVEN(int num){
if(num%2==0){
return true;
}
else{
return false;
}
}
public boolean isPOSITIVE(int num){
if(num>0){
return true;
}
else{
return false;
}
}
}
public class ButtonHandlerCANCEL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Program terminating...");
System.exit(0);
}
}
public static void main(String[] args) {
new NumberGUI();
}
}
ODD-POSITIVE
ODD-NEGATIVE
EVEN-POSITIVE
EVEN-NEGATIVE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NumberGUI extends JFrame{
private static String str="";
private JLabel jl1,jlResult;
private JTextField jt1;
private JButton btnOK,btnCANCEL;
private ButtonHandlerOK btnok;
private ButtonHandlerCANCEL btncancel;
public NumberGUI(){
jl1=new JLabel("Enter a number: ");
jlResult=new JLabel();
jt1=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
btnok=new ButtonHandlerOK();
btnOK.addActionListener(btnok);
btncancel=new ButtonHandlerCANCEL();
btnCANCEL.addActionListener(btncancel);
Container pane= new Container();
pane.setLayout(new GridLayout(3,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(btnCANCEL);
pane.add(jlResult);
add(pane);
setTitle("Amante Inc.");
setSize(500,180);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public class ButtonHandlerOK implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int num=0;
num=Integer.parseInt(jt1.getText());
String display=ODDEVEN(num);
jlResult.setText(num + " is "+display);
}
public String ODDEVEN(int num){
if(num==0){
str="";
}
else if(num%2==0){
str="EVEN";
}
else {
str="ODD";
}
NEGAPOS(num);
return str;
}
public String NEGAPOS(int num){
if(num<0){
str=str+"-NEGATIVE";
}
else if(num>0){
str=str+"-POSITIVE";
}
else{
str=str+"ZERO";
}
return str;
}
public boolean isEVEN(int num){
if(num%2==0){
return true;
}
else{
return false;
}
}
public boolean isPOSITIVE(int num){
if(num>0){
return true;
}
else{
return false;
}
}
}
public class ButtonHandlerCANCEL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Program terminating...");
System.exit(0);
}
}
public static void main(String[] args) {
new NumberGUI();
}
}
LabExer#39
39. Create a program that will get the sum of the two numbers and determine if it is divisible by 5.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TheSumGUI extends JFrame{
private JLabel jl1,jl2,jlResult;
private JTextField jt1,jt2;
private JButton btnOK,btnCANCEL;
private ButtonHandlerOK btnok;
private ButtonHandlerCANCEL btncancel;
public TheSumGUI(){
jl1=new JLabel("Enter 1st Number: ");
jl2=new JLabel("Enter 2nd Number: ");
jlResult=new JLabel();
jt1=new JTextField(20);
jt2=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
btnok=new ButtonHandlerOK();
btnOK.addActionListener(btnok);
btncancel=new ButtonHandlerCANCEL();
btnCANCEL.addActionListener(btncancel);
Container pane= new Container();
pane.setLayout(new GridLayout(3,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(jl2);
pane.add(jt2);
pane.add(btnCANCEL);
pane.add(jlResult);
add(pane);
setTitle("Amante Inc.");
setSize(650,180);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public class ButtonHandlerOK implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int num1,num2;
num1=Integer.parseInt(jt1.getText());
num2=Integer.parseInt(jt2.getText());
if((num1+num2)%5==0){
jlResult.setText("The sum is: "+(num1+num2)+ " and is divisible by 5.");
}
else{
jlResult.setText("The sum is: "+(num1+num2)+ " and not divisible by 5.");
}
}
}
public class ButtonHandlerCANCEL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Program terminating...");
System.exit(0);
}
}
public static void main(String[] args) {
new TheSumGUI();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TheSumGUI extends JFrame{
private JLabel jl1,jl2,jlResult;
private JTextField jt1,jt2;
private JButton btnOK,btnCANCEL;
private ButtonHandlerOK btnok;
private ButtonHandlerCANCEL btncancel;
public TheSumGUI(){
jl1=new JLabel("Enter 1st Number: ");
jl2=new JLabel("Enter 2nd Number: ");
jlResult=new JLabel();
jt1=new JTextField(20);
jt2=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
btnok=new ButtonHandlerOK();
btnOK.addActionListener(btnok);
btncancel=new ButtonHandlerCANCEL();
btnCANCEL.addActionListener(btncancel);
Container pane= new Container();
pane.setLayout(new GridLayout(3,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(jl2);
pane.add(jt2);
pane.add(btnCANCEL);
pane.add(jlResult);
add(pane);
setTitle("Amante Inc.");
setSize(650,180);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public class ButtonHandlerOK implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
int num1,num2;
num1=Integer.parseInt(jt1.getText());
num2=Integer.parseInt(jt2.getText());
if((num1+num2)%5==0){
jlResult.setText("The sum is: "+(num1+num2)+ " and is divisible by 5.");
}
else{
jlResult.setText("The sum is: "+(num1+num2)+ " and not divisible by 5.");
}
}
}
public class ButtonHandlerCANCEL implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null,"Program terminating...");
System.exit(0);
}
}
public static void main(String[] args) {
new TheSumGUI();
}
}
LabExer#38
38. Design the following GUI in Java using JFrame and other components.
Enter first number : OK
Enter second number : CANCEL
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleGUI extends JFrame{
private JLabel jl1,jl2;
private JTextField jt1,jt2;
private JButton btnOK,btnCANCEL;
public SampleGUI(){
jl1=new JLabel("Enter 1st Number: ");
jl2=new JLabel("Enter 2nd Number: ");
jt1=new JTextField(20);
jt2=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
Container pane= new Container();
pane.setLayout(new GridLayout(2,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(jl2);
pane.add(jt2);
pane.add(btnCANCEL);
add(pane);
setTitle("Amante Inc.");
setSize(500,150);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new SampleGUI();
}
}
Enter first number : OK
Enter second number : CANCEL
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SampleGUI extends JFrame{
private JLabel jl1,jl2;
private JTextField jt1,jt2;
private JButton btnOK,btnCANCEL;
public SampleGUI(){
jl1=new JLabel("Enter 1st Number: ");
jl2=new JLabel("Enter 2nd Number: ");
jt1=new JTextField(20);
jt2=new JTextField(20);
btnOK=new JButton("OK");
btnCANCEL=new JButton("CANCEL");
Container pane= new Container();
pane.setLayout(new GridLayout(2,2));
pane.add(jl1);
pane.add(jt1);
pane.add(btnOK);
pane.add(jl2);
pane.add(jt2);
pane.add(btnCANCEL);
add(pane);
setTitle("Amante Inc.");
setSize(500,150);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new SampleGUI();
}
}
LabExer#37
37. Develop program that has the following methods: ODD(), EVEN(), NEGATIVE(), POSITIVE(). Each method will be visited in order
of sequence as above stated. Every visit will print the string or word the same name of the class if the number entered is TRUE.
Otherwise, no value is returned or NULL. Then, call each method until it reaches the method name POSTIVE() will then stop the
whole process and display the final output.
Example#1:
Enter a number: 3
3 is ODD POSITIVE
Example#2:
Enter a number: -4
-4 IS EVEN NEGATIVE
import javax.swing.JOptionPane;
public class Number {
private static String str="";
public static void main(String[] args) {
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
String display=ODDEVEN(num);
JOptionPane.showMessageDialog(null, num+" is "+display);
}
public static String ODDEVEN(int num){
if(num==0){
str="";
}
else if(num%2==0){
str="EVEN";
}
else {
str="ODD";
}
NEGAPOS(num);
return str;
}
public static String NEGAPOS(int num){
if(num<0){
str=str+"-NEGATIVE";
}
else if(num>0){
str=str+"-POSITIVE";
}
else{
str=str+"ZERO";
}
return str;
}
public static boolean isEVEN(int num){
if(num%2==0){
return true;
}
else{
return false;
}
}
public static boolean isPOSITIVE(int num){
if(num>0){
return true;
}
else{
return false;
}
}
}
of sequence as above stated. Every visit will print the string or word the same name of the class if the number entered is TRUE.
Otherwise, no value is returned or NULL. Then, call each method until it reaches the method name POSTIVE() will then stop the
whole process and display the final output.
Example#1:
Enter a number: 3
3 is ODD POSITIVE
Example#2:
Enter a number: -4
-4 IS EVEN NEGATIVE
import javax.swing.JOptionPane;
public class Number {
private static String str="";
public static void main(String[] args) {
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
String display=ODDEVEN(num);
JOptionPane.showMessageDialog(null, num+" is "+display);
}
public static String ODDEVEN(int num){
if(num==0){
str="";
}
else if(num%2==0){
str="EVEN";
}
else {
str="ODD";
}
NEGAPOS(num);
return str;
}
public static String NEGAPOS(int num){
if(num<0){
str=str+"-NEGATIVE";
}
else if(num>0){
str=str+"-POSITIVE";
}
else{
str=str+"ZERO";
}
return str;
}
public static boolean isEVEN(int num){
if(num%2==0){
return true;
}
else{
return false;
}
}
public static boolean isPOSITIVE(int num){
if(num>0){
return true;
}
else{
return false;
}
}
}
LabExer#35
35. Given two int values, return their sum. Unless the two values are the same, then return double their sum.
sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8
import javax.swing.*;
public class ReturnSum {
public static void main(String[] args) {
int num1=Integer.parseInt(JOptionPane.showInputDialog("Enter first integer: "));
int num2=Integer.parseInt(JOptionPane.showInputDialog("Enter second integer: "));
int sum1=method(num1,num2);
JOptionPane.showMessageDialog(null, sum1);
}
public static int method(int x,int y){
int sum=x+y;
int doubleSum=sum*2;
if(x!=y){
return sum;
}
else{
return doubleSum;
}
}
}
sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8
import javax.swing.*;
public class ReturnSum {
public static void main(String[] args) {
int num1=Integer.parseInt(JOptionPane.showInputDialog("Enter first integer: "));
int num2=Integer.parseInt(JOptionPane.showInputDialog("Enter second integer: "));
int sum1=method(num1,num2);
JOptionPane.showMessageDialog(null, sum1);
}
public static int method(int x,int y){
int sum=x+y;
int doubleSum=sum*2;
if(x!=y){
return sum;
}
else{
return doubleSum;
}
}
}
Biyernes, Setyembre 20, 2013
LabExer#30
30. Using Vector class, write the same process program above.
import java.util.Vector;
import javax.swing.JOptionPane;
public class ShoppingList{
public static void main(String[] args) {
Vector amante=new Vector();
String choice []= { "Add an item at specific location in the list", "Delete an item in the list", "Print the contents of the vector","Delete all elements","Exit"};
String choice1,item;
int index;
for(int a=0;a<4;a++){
item=JOptionPane.showInputDialog("Enter item: ");
amante.add(item);
}
do{
String display = "";
choice1 = (String) JOptionPane.showInputDialog(null,
"Welcome! Please select an option.",
"Options",JOptionPane.QUESTION_MESSAGE,null, choice,choice[0]);
if (choice1.compareTo(choice[0]) == 0){
index=Integer.parseInt(JOptionPane.showInputDialog("Enter specified index: "));
item=JOptionPane.showInputDialog("Enter item: ");
amante.remove(index);
amante.add(index,item);
}
else if (choice1.compareTo(choice[1]) == 0){
index=Integer.parseInt(JOptionPane.showInputDialog("Enter specified index of the item to delete: "));
amante.remove(index);
amante.add(index,null);
}
else if (choice1.compareTo(choice[2]) == 0){
for(int y=0;y<4;y++){
display=display+"Item["+y+"] - "+amante.get(y)+"\n";
}
JOptionPane.showMessageDialog(null, display);
}
else if (choice1.compareTo(choice[3]) == 0){
for(int z=0;z<4;z++){
amante.remove(z);
amante.add(z,null);
}
}
}
while(!choice1.equals("Exit"));
JOptionPane.showMessageDialog(null, "Thank you for using the program! We hope to serve you next time!","Amante Programs Inc.",JOptionPane.PLAIN_MESSAGE);
}
}
import java.util.Vector;
import javax.swing.JOptionPane;
public class ShoppingList{
public static void main(String[] args) {
Vector amante=new Vector();
String choice []= { "Add an item at specific location in the list", "Delete an item in the list", "Print the contents of the vector","Delete all elements","Exit"};
String choice1,item;
int index;
for(int a=0;a<4;a++){
item=JOptionPane.showInputDialog("Enter item: ");
amante.add(item);
}
do{
String display = "";
choice1 = (String) JOptionPane.showInputDialog(null,
"Welcome! Please select an option.",
"Options",JOptionPane.QUESTION_MESSAGE,null, choice,choice[0]);
if (choice1.compareTo(choice[0]) == 0){
index=Integer.parseInt(JOptionPane.showInputDialog("Enter specified index: "));
item=JOptionPane.showInputDialog("Enter item: ");
amante.remove(index);
amante.add(index,item);
}
else if (choice1.compareTo(choice[1]) == 0){
index=Integer.parseInt(JOptionPane.showInputDialog("Enter specified index of the item to delete: "));
amante.remove(index);
amante.add(index,null);
}
else if (choice1.compareTo(choice[2]) == 0){
for(int y=0;y<4;y++){
display=display+"Item["+y+"] - "+amante.get(y)+"\n";
}
JOptionPane.showMessageDialog(null, display);
}
else if (choice1.compareTo(choice[3]) == 0){
for(int z=0;z<4;z++){
amante.remove(z);
amante.add(z,null);
}
}
}
while(!choice1.equals("Exit"));
JOptionPane.showMessageDialog(null, "Thank you for using the program! We hope to serve you next time!","Amante Programs Inc.",JOptionPane.PLAIN_MESSAGE);
}
}
Mag-subscribe sa:
Mga Post (Atom)