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();
}
}
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);
}
}
LabExer#29
29. Using ArrayList, write a menu based program that accepts a shopping list of four items from the command line and store in a Vector and perform the following operations.
a. To add an item at specific location in the list.
b. To delete an item in the list
c. To print the contents of the vector
d. To delete all elements
e. To add an item at the end of the vector
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ShoppingList {
public static void main(String[] args) {
ArrayList amante=new ArrayList();
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);
}
}
a. To add an item at specific location in the list.
b. To delete an item in the list
c. To print the contents of the vector
d. To delete all elements
e. To add an item at the end of the vector
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ShoppingList {
public static void main(String[] args) {
ArrayList amante=new ArrayList();
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);
}
}
LabExer#27
27. Write a Java program for sorting a given list of names in ascending order.
import java.util.Arrays;
import javax.swing.JOptionPane;
public class LabExer27 {
public static void main(String[] args) {
String display="";
String names[]=new String[5];
for(int x=0;x<5;x++){
names[x]=JOptionPane.showInputDialog("Enter name: ");
}
Arrays.sort(names);
for (String number: names) {
display=display+number+ "\n";
}
JOptionPane.showMessageDialog(null,"The sorted Strings are:\n"+ display);
}
}
import java.util.Arrays;
import javax.swing.JOptionPane;
public class LabExer27 {
public static void main(String[] args) {
String display="";
String names[]=new String[5];
for(int x=0;x<5;x++){
names[x]=JOptionPane.showInputDialog("Enter name: ");
}
Arrays.sort(names);
for (String number: names) {
display=display+number+ "\n";
}
JOptionPane.showMessageDialog(null,"The sorted Strings are:\n"+ display);
}
}
LabExer#26
26. Program to display multiplication tables from 1 to 10 using 2d array.
public class MultiplicationTable {
public static void main(String[] args) {
System.out.println("\tMultiplication table: up to 12");
for (int i=1; i<=10; i++) {
for (int j=1; j<=10; j++) {
System.out.print(addSpaces(4, i*j));
}
System.out.print("\n");
}
}
static String addSpaces(int spaces, int n) {
String s="";
for (int i=(""+n).length(); i<spaces; i++)
s+=" ";
return s+n;
}
}
public class MultiplicationTable {
public static void main(String[] args) {
System.out.println("\tMultiplication table: up to 12");
for (int i=1; i<=10; i++) {
for (int j=1; j<=10; j++) {
System.out.print(addSpaces(4, i*j));
}
System.out.print("\n");
}
}
static String addSpaces(int spaces, int n) {
String s="";
for (int i=(""+n).length(); i<spaces; i++)
s+=" ";
return s+n;
}
}
LabExer#22
22. Program to search an element in an array. Ask-before-iterating loop sets how many elements to be inserted. Afterwards, use the loop to store values/data from user. When the loop ends, ask the user to search for an element. If an element is found, display it is found and where position it is being located. Otherwise, display element is not found.
import javax.swing.JOptionPane;
public class LabExer22 {
public static void main(String[] args) {
int limit=Integer.parseInt(JOptionPane.showInputDialog("Enter how many elements to be inserted: "));
String elements[]=new String [limit];
String search="";
int base=0;
int index=0;
String found="Element is found at index ";
String notFound="Element is not found";
for(int a=0;a<limit;a++){
elements[a]=JOptionPane.showInputDialog("Enter data: ");
}
search=JOptionPane.showInputDialog("Thank You for entering data!\n"+"Please enter an element to search: ");
search=search.toLowerCase();
for(int x=0;x<limit;x++){
if(elements[x].toLowerCase().equals(search)){
base++;
index=x;
}
}
if(base>0){
JOptionPane.showMessageDialog(null, found +index,"AMANTE's Program",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, notFound,"AMANTE's Program",JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(null,"Thank You! I hope to serve you next time..","AMANTE's Program",JOptionPane.PLAIN_MESSAGE);
}
}
import javax.swing.JOptionPane;
public class LabExer22 {
public static void main(String[] args) {
int limit=Integer.parseInt(JOptionPane.showInputDialog("Enter how many elements to be inserted: "));
String elements[]=new String [limit];
String search="";
int base=0;
int index=0;
String found="Element is found at index ";
String notFound="Element is not found";
for(int a=0;a<limit;a++){
elements[a]=JOptionPane.showInputDialog("Enter data: ");
}
search=JOptionPane.showInputDialog("Thank You for entering data!\n"+"Please enter an element to search: ");
search=search.toLowerCase();
for(int x=0;x<limit;x++){
if(elements[x].toLowerCase().equals(search)){
base++;
index=x;
}
}
if(base>0){
JOptionPane.showMessageDialog(null, found +index,"AMANTE's Program",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, notFound,"AMANTE's Program",JOptionPane.INFORMATION_MESSAGE);
}
JOptionPane.showMessageDialog(null,"Thank You! I hope to serve you next time..","AMANTE's Program",JOptionPane.PLAIN_MESSAGE);
}
}
LabExer#21
21. Create a program that will search for the highest and lowest value in an array of 10 elements.
import javax.swing.JOptionPane;
public class HighLowArray{
public static void main(String[]args){
int x[]=new int[10];
for(int a=0;a<x.length;a++){
x[a]=Integer.parseInt(JOptionPane.showInputDialog("Enter num["+(a)+"]"));
}
int high=x[0];
int low=x[0];
for(int y=0;y<10;y++)
{
if(high<x[y]) high=x[y];
if(low>x[y]) low=x[y];
}
JOptionPane.showMessageDialog(null,"The highest number is: " +high);
JOptionPane.showMessageDialog(null,"The lowest number is: " +low);
}
}
import javax.swing.JOptionPane;
public class HighLowArray{
public static void main(String[]args){
int x[]=new int[10];
for(int a=0;a<x.length;a++){
x[a]=Integer.parseInt(JOptionPane.showInputDialog("Enter num["+(a)+"]"));
}
int high=x[0];
int low=x[0];
for(int y=0;y<10;y++)
{
if(high<x[y]) high=x[y];
if(low>x[y]) low=x[y];
}
JOptionPane.showMessageDialog(null,"The highest number is: " +high);
JOptionPane.showMessageDialog(null,"The lowest number is: " +low);
}
}
LabExer#17
17. Define an enumeration for each of the months in the year. Use a for-each statement to display each month.
import javax.swing.JOptionPane;
public class amante{
public static void main (String[]args){
String display="";
String []months=new String[]{"January","February","March","April","May","June","July","August","September","October","November","December"};
for(String out:months){
display=display+out+"\n";
}
JOptionPane.showMessageDialog(null, display);
}
}
import javax.swing.JOptionPane;
public class amante{
public static void main (String[]args){
String display="";
String []months=new String[]{"January","February","March","April","May","June","July","August","September","October","November","December"};
for(String out:months){
display=display+out+"\n";
}
JOptionPane.showMessageDialog(null, display);
}
}
LabExer#15
16. Write a loop that will create a new string that is the reverse of a given string.
import java.util.*;
import javax.swing.JOptionPane;
public class ReversedString {
public static void main(String[] args) {
String str;
Scanner input=new Scanner(System.in);
str=JOptionPane.showInputDialog("Enter String: ");
str=str.toLowerCase();
String reverse="";
for(int x=str.length()-1;x>=0;x--){
reverse=reverse+str.charAt(x);
}
System.out.println("Original string: "+str);
System.out.println("Reversed string: "+reverse);
}
}
import java.util.*;
import javax.swing.JOptionPane;
public class ReversedString {
public static void main(String[] args) {
String str;
Scanner input=new Scanner(System.in);
str=JOptionPane.showInputDialog("Enter String: ");
str=str.toLowerCase();
String reverse="";
for(int x=str.length()-1;x>=0;x--){
reverse=reverse+str.charAt(x);
}
System.out.println("Original string: "+str);
System.out.println("Reversed string: "+reverse);
}
}
LabExer#15
15. Write a loop that will count the number of blank characters in a given string.
import javax.swing.JOptionPane;
public class BlankChar {
public static void main(String[] args) {
String str;
int a=0;
str=JOptionPane.showInputDialog("Enter String: ");
str=str.toLowerCase();
for(int x=0;x<=str.length()-1;x++){
if(str.charAt(x)==' '){
a++;
}
else{
}
}
JOptionPane.showMessageDialog(null, "The number of blank characters in the given string is: "+a);
}
}
import javax.swing.JOptionPane;
public class BlankChar {
public static void main(String[] args) {
String str;
int a=0;
str=JOptionPane.showInputDialog("Enter String: ");
str=str.toLowerCase();
for(int x=0;x<=str.length()-1;x++){
if(str.charAt(x)==' '){
a++;
}
else{
}
}
JOptionPane.showMessageDialog(null, "The number of blank characters in the given string is: "+a);
}
}
LabExer#14
14. Write a for statement to compute the sum 1 + 2² + 3² + 4² + 5² + ... + n².
import javax.swing.JOptionPane;
public class TheSum{
public static void main(String[] args){
int a=1,b=2,n,num=0,total=0;
n=Integer.parseInt(JOptionPane.showInputDialog("How many odd integers do you want to display? "));
for (int i=0;i<n;i++){
num=a*a;
System.out.print(a + "² + ");
a=a+b;
total=total+num;
}
System.out.println(" ");
System.out.println("The sum is: "+total);
}
}
import javax.swing.JOptionPane;
public class TheSum{
public static void main(String[] args){
int a=1,b=2,n,num=0,total=0;
n=Integer.parseInt(JOptionPane.showInputDialog("How many odd integers do you want to display? "));
for (int i=0;i<n;i++){
num=a*a;
System.out.print(a + "² + ");
a=a+b;
total=total+num;
}
System.out.println(" ");
System.out.println("The sum is: "+total);
}
}
LabExer#13
13. Write a fragment of code that will compute the sum of the first n positive odd integers. For example, if n is 5, you should compute 1 + 3 + 5 + 7 + 9.
import javax.swing.JOptionPane;
public class OddIntegers{
public static void main(String[] args){
int a=1,b=2,n,num=0;
n=Integer.parseInt(JOptionPane.showInputDialog("How many odd integers do you want to display? "));
for (int i=0;i<n;i++){
System.out.print(a + " + ");
a=a+b;
num=n*n;
}
System.out.println(" ");
System.out.println("The sum of all odd integers is: "+num);
}
}
import javax.swing.JOptionPane;
public class OddIntegers{
public static void main(String[] args){
int a=1,b=2,n,num=0;
n=Integer.parseInt(JOptionPane.showInputDialog("How many odd integers do you want to display? "));
for (int i=0;i<n;i++){
System.out.print(a + " + ");
a=a+b;
num=n*n;
}
System.out.println(" ");
System.out.println("The sum of all odd integers is: "+num);
}
}
LabExer#11
11. Develop a program for computing the month-by-month balance in your savings account. You can make one transaction-a deposit or a withdrawal-each month. Interest is added to the account at the beginning of each month. The monthly interest rate is the yearly percentage rate divided by 12.
Use the following formula:yearlyRate=0.025.
balance = balance+(yearlyRate / 12) * balance
Sample Output:
Enter d for deposit, w for withdrawal or x to exit: d
Beginning of Month Balance: 0.0
Enter deposit amount: 5000
Balance after transaction: 5000.0
Enter d for deposit, w for withdrawal or x to exit: d
Beginning of Month Balance: 5010.416666666667
Enter deposit amount: 3000
Balance after transaction: 8010.416666666667
Enter d for deposit, w for withdrawal or x to exit: w
Beginning of Month Balance: 8027.105034722223
Enter withdrawal amount: 2000
Balance after transaction: 6027.105034722223
Enter d for deposit, w for withdrawal or x to exit: e
Beginning of Month Balance: 6039.66150354456
Balance after transaction: 6039.66150354456
import javax.swing.JOptionPane;
public class wee{
public static void SavingsAccount (String[]args){
double yearlyRate=0.025, balance=0,balance1=0,withdrawal=0;
String choice="";
do{
choice=JOptionPane.showInputDialog(null, "Please enter choice.. \n\nD. Deposit \nW. Withdrawal \nE. Exit" + "\n\nBeginning of Month Balance: "+balance1);
switch(choice.toLowerCase()){
case "d":
balance=Double.parseDouble(JOptionPane.showInputDialog("Enter deposit amount: "));
balance1=balance1+balance;
JOptionPane.showMessageDialog(null, "Balance after transaction: "+balance1);
balance1 = balance1+(yearlyRate / 12) * balance1;
break;
case "w":
withdrawal=Double.parseDouble(JOptionPane.showInputDialog("Enter amount: "));
if(balance1>=withdrawal){
balance1=balance1-withdrawal;
JOptionPane.showMessageDialog(null, "Balance after transaction: "+balance1);
balance1 = balance1+(yearlyRate / 12) * balance1;
}
else{
JOptionPane.showMessageDialog(null, "Insufficient Funds!", "Alert", JOptionPane.ERROR_MESSAGE);
}
break;
}
}
while(!(choice.toLowerCase().equals("e")));
JOptionPane.showMessageDialog(null, "Balance after transaction: "+balance1);
}
}
Mag-subscribe sa:
Mga Post (Atom)