Sunday, September 14, 2014

Visitors2Cash

Visitors2Cash

http://Visitors2Cash.com/ref.php?refId=180099

Visitors2Cash advertising media is a leading online advertising company.
With Visitors2Cash.com's money making system you can easily earn money
by sharing referral links to your friends, family and other people through
websites, blogs, forums, social media, facebook wall, facebook pages, groups,
twitter, chat rooms and other media.

Wednesday, September 10, 2014

Read Inverse

public static void main(String args[]) throws IOException { String num[] = new String[5]; boolean choose = false; String ask; int l = num.length; BufferedReader z = new BufferedReader(new InputStreamReader(System.in)); while (!choose) { for (int i = 0; i < l; i++) { System.out.print(" Enter a number or String : "); num[i] = z.readLine(); } System.out.println(" The oppossing order of the given numbers are : "); for (int i = l - 1; i >= 0;) { System.out.print(" " + num[i]); i--; } System.out.println(" \n" + "Do you want to try again ? (y/n) "); ask = z.readLine(); if (ask.equals("y")) { choose = false; } else if (ask.equals("n")) { choose = true; } } } }

BirthDAy

public static void main (String args []) throws IOException{ int day, month ,year; BufferedReader z = new BufferedReader( new InputStreamReader(System.in)); System.out.println( " ENTER DATA AS NEEDED " + "\n"); System.out.print(" Enter day in number : "); day = Integer.parseInt(z.readLine()); System.out.print(" Enter month in number : "); month = Integer.parseInt(z.readLine()); System.out.print(" Enter year in number : "); year = Integer.parseInt(z.readLine()); if(month == 1){ System.out.println("January" + " " + day + " , " + year); }else if(month==2){ System.out.println("February"+" " + day + " , "+ year); }else if(month==3){ System.out.println("March"+" " + day + " , "+ year); } else if(month==4){ System.out.println("April"+" " + day + " , "+ year); } else if(month==5){ System.out.println("May"+" " + day + " , "+ year); } else if(month==6){ System.out.println("June"+" " + day + " , "+ year); } else if(month==7){ System.out.println("July"+" " + day + " , "+ year); } else if(month==8){ System.out.println("August"+" " + day + " , "+ year); } else if(month==9){ System.out.println("September"+" " + day + " , "+ year); } else if(month==10){ System.out.println("October"+" " + day + " , "+ year); } else if(month==11){ System.out.println("November"+" " + day + " , "+ year); } else if(month==12){ System.out.println("December"+ " " + day + " , "+ year); } } }

Quick Sort

private static class Quicksort { int array[]; int size; public Quicksort(int n) { size = n; //create array with size n+1 array = new int[n + 1]; //asign value into the array for (int i = 0; i < n; i++) { array[i] = (int) Math.round(Math.random() * 89 + 10); } //set the last element as big value array[n] = 99999; } public int partition(int p, int q) { int i = p; int j = q + 1; // Get the pivot element from the middle of the list int pivot = array[p]; // Divide into two lists do { // If the current value from the left list is smaller then the pivot // element then get the next element from the left list do { i++;// As we not get we can increase i } while (array[i] < pivot); // If the current value from the right list is larger then the pivot // element then get the next element from the right list do { j--;// As we not get we can increase j } while (array[j] > pivot); // If we have found a values in the left list which is larger then // the pivot element and if we have found a value in the right list // which is smaller then the pivot element then we exchange the values. if (i < j) { swap(i, j); } } while (i < j); //swap the pivote element and j th element swap(p, j); return j; } private void swap(int p, int j) { //exachage the elements int temp = array[p]; array[p] = array[j]; array[j] = temp; } public void quicksort() { // Recursion quicksort(0, size - 1); } public void quicksort(int p, int q) { int j; if (p < q) { // Divide into two lists j = partition(p, q); // Recursion quicksort(p, j - 1); quicksort(j + 1, q); } } public void print() { //print the elements of array for (int i = 0; i < size; i++) { System.out.print(array[i] + " | "); } System.out.println(); } public static void main(String args[]) { Quicksort q = new Quicksort(15); System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<"); q.print(); q.quicksort(); System.out.println("After Sort > > > > > > > > > > > >"); q.print(); System.out.println("=======+============+=======+============+=======+============"); Quicksort q2 = new Quicksort(125); System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<"); q2.print(); q2.quicksort(); System.out.println("After Sort > > > > > > > > > > > >"); q2.print(); System.out.println("=======+============+=======+============+=======+============"); Quicksort q3 = new Quicksort(5); System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<"); q3.print(); q3.quicksort(); System.out.println("After Sort > > > > > > > > > > > >"); q3.print(); } } }

Natoinal Wedgits Payroll

public static void main(String[] args) { double d_Employee1, d_Employee2, d_Employee3, d_Employee4, d_Employee5; double hrs1 = 40, hrs2 = 24, hrs3 = 10, hrs4 = 5, hrs5 = 35; double d_rate = 9.50, d_rate2 = 8.75, d_rate3 = 10.95, d_rate4 = 6.75, d_rate5 = 15.50; double gross1, gross2, gross3, gross4, gross5; double tax1, tax2, tax3, tax4, tax5; double net1, net2, net3, net4, net5; double Total_Gross, Total_tax, Total_net; d_Employee1 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_Employee1")); d_Employee2 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_Employee2")); d_Employee3 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_Employee3")); d_Employee4 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_Employee4")); d_Employee5 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_Employee5")); d_rate = Double.parseDouble(JOptionPane.showInputDialog("Enter d_rate")); d_rate2 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_rate2")); d_rate3 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_rate3")); d_rate4 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_rate4")); d_rate5 = Double.parseDouble(JOptionPane.showInputDialog("Enter d_rate5")); gross1 = hrs1 * d_rate; gross2 = hrs2 * d_rate2; gross3 = hrs3 * d_rate3; gross4 = hrs4 * d_rate4; gross5 = hrs5 * d_rate5; tax1 = .35 * gross1; tax2 = .35 * gross2; tax3 = .35 * gross3; tax4 = .35 * gross4; tax5 = .35 * gross5; net1 = gross1 - tax1; net2 = gross2 - tax2; net3 = gross3 - tax3; net4 = gross4 - tax4; net5 = gross5 - tax5; Total_Gross = gross1 + gross2 + gross3 + gross4 + gross5; Total_tax = tax1 + tax2 + tax3 + tax4 + tax5; Total_net = net1 + net2 + net3 + net4 + net5; System.out.print("National Wiidgets"); System.out.println(); System.out.print("Payroll Report"); System.out.println(); System.out.print("Employee no.\t\t Hrs\t\t Rate\t\t Gross\t\t\t Tax\t\t\t Net"); System.out.println(); System.out.println("----------------------------------------------------------------------------------------------------------------------"); System.out.println(d_Employee1 + "\t\t\t" + hrs1 + "\t\t" + d_rate + "\t\t" + gross1 + "\t\t\t" + tax1 + "\t\t\t" + net1); System.out.println(); System.out.println(d_Employee2 + "\t\t\t" + hrs2 + "\t\t" + d_rate2 + "\t\t" + gross2 + "\t\t\t" + tax2 + "\t\t\t" + net2); System.out.println(); System.out.println(d_Employee3 + "\t\t\t" + hrs3 + "\t\t" + d_rate3 + "\t\t" + gross3 + "\t\t\t" + tax3 + "\t\t\t" + net3); System.out.println(); System.out.println(d_Employee4 + "\t\t\t" + hrs4 + "\t\t" + d_rate4 + "\t\t" + gross4 + "\t\t\t" + tax4 + "\t\t\t" + net4); System.out.println(); System.out.println(d_Employee5 + "\t\t\t" + hrs5 + "\t\t" + d_rate5 + "\t\t" + gross5 + "\t\t\t" + tax5 + "\t\t\t" + net5); System.out.println("----------------------------------------------------------------------------------------------------------------------"); System.out.println(); Total_Gross = gross1 + gross2 + gross3 + gross4 + gross5; Total_tax = tax1 + tax2 + tax3 + tax4 + tax5; Total_net = net1 + net2 + net3 + net4 + net5; System.out.println("\t\t\t\t\t\tTotal Gross is: " + Total_Gross + "\tTotal Tax is: " + Total_tax + "\tTotal Net is: " + Total_net); } }

Sunday, August 24, 2014

NameValidation

Class Name: NameValidation. Write a fragment of code that will read words from the keyboard until the word exit is entered. For each word except exit, report whether its first character is equal to its last character.

import java.util.*;
public class NameValidation {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
       
        boolean done = false;
        String str = "exit";
       
       while (!done){
        System.out.print("Enter a word: \t");
        String str2 = input.next();
        str2 = str2.toLowerCase();
       
            int number = str2.length();
            int number1 = 0;
           
            String x = str2.substring(number1,1);
            String y = str2.substring((number - 1), number);
           
            System.out.println();
            if (str.equals(str2)) {
                System.out.println("Program is now terminating...");
                done = true;
            }
            else {
                done = false; 
               
            if (x.equals(y)){
                System.out.println("The first character is equal to the last character: " + str2);
            }
            else {
                System.out.println("The first character is not equal to the last character: " + str2);
            }   
            }
        }
    }
}

Bank Transaction

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

import javax.swing.JOptionPane;
public class LabExercise11 {
    public static void main(String[] args)
    {
        double yearlyRate=0.025;
        double balance=0.0;
        double amount;
        double withdraw;
        String menu;    

       do
        { 
           menu=JOptionPane.showInputDialog("[D]eposit \n[W]ithdraw \n[E]xit \nBeginning of Month Balance: "+balance);
            
            menu = menu.toLowerCase();
       switch(menu.charAt(0))
        {
       case 'd':
   
        if(balance == 0.0)
         {
         amount=Double.parseDouble(JOptionPane.showInputDialog("\nEnter Deposit Amount: "));
         JOptionPane.showMessageDialog(null,"Balance after transaction: "+amount);
         balance= amount+(yearlyRate / 12) *amount;
    
         }
         else
         {
         amount=Double.parseDouble(JOptionPane.showInputDialog("\nEnter Deposit Amount: "));
         balance=amount+balance;
         JOptionPane.showMessageDialog(null,"Balance after transaction: "+balance);
         balance = balance+(yearlyRate / 12) * balance;
         }
        break;
       
        case 'w':
                   if(balance == 0.0)
                    {
                  JOptionPane.showMessageDialog(null,"Invalid Transaction. Empty balance.");
                    }
                    else
                    {
                      withdraw=Double.parseDouble (JOptionPane.showInputDialog("Enter Withdrawal Amount:"));  
                        if(withdraw>balance)
                        {      
                        JOptionPane.showMessageDialog(null, "Amount is beyond balance.","Invalid Transaction!", 0);
                        }
                        else
                        {
                            balance=balance-withdraw;  
                            JOptionPane.showMessageDialog(null,"You have successfully withdrawed "+withdraw);
                        }
                    }
                break;
            case 'e':
            JOptionPane.showMessageDialog(null,"Program is now terminating...");
            System.exit(0);
            break;
            default:
            JOptionPane.showMessageDialog(null,"Invalid choice."); 
              }        
           }   while(!menu.equals("e"));
        } 
    }

MileAge

Class Name: MileAge. Translate the algorithm below into Java. Don’t forget to declare variables before they are used.

import java.util.Scanner;
public class MileAge {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        double miles=0;
        double gallons=0;
        double tot=0;
       
        System.out.print("This program will calculate mileage.");
        System.out.print("\nEnter miles driven: ");
        miles=input.nextInt();
        System.out.print("Enter gallons used: ");
        gallons=input.nextInt();
       
        tot = miles/gallons;
       
        System.out.print("Miles per Gallon: "+tot+" mi/gal");
    }
}

DegreeConverter

Class Name: DegreeConverter. Develop a program that will convert Fahrenheit temperature into Celsius temperature. Enter desired Fahrenheit value. Use the formula: c=(f-32)/1.8.

import java.util.*;

public class DegreeConverter {
    public static void main (String []args){
        Scanner input = new Scanner(System.in);
       
        double c;
        System.out.print("Enter temperature(Fahrenheit): ");
        int temp = input.nextInt();
               
        c = (temp-32)/1.8;
       
        System.out.println("In Celsius: "+c);
    }
}







Observation:
It Converts Degree from Fahrenheit to Celsius.
Recommendation:
You really need to have the formula in converting degrees.

Thursday, August 21, 2014

People's park

People’s park implemented entrance fees for children ages 2 and below 8, is 5 pesos. 7 pesos for 17 and below. And, 10 pesos for the rest of the ages. Calculate how much will the visitor’s pay?

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class LabExercise42 extends javax.swing.JFrame {

    public LabExercise42() {
        initComponents();
    }

    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Entrance Fee:");

        jLabel1.setText("Age:");

        jComboBox1.setMaximumRowCount(5);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2-8", "9-17", "18-above" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jLabel3.setText("PEOPLE'S PARK");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(60, 60, 60)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(59, 59, 59)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       
        Object choice = jComboBox1.getSelectedItem();
        choice = choice.toString();
       
        if(choice == "2-8"){
            jTextField1.setText("P5.00");
        }else if (choice == "9-17"){
            jTextField1.setText("P7.00");
        }else if (choice == "18-above"){
            jTextField1.setText("P10.00");
        }
}

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {

}

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

}

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LabExercise42().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;

}

Add/Divisble by 5

Create a program that will get the sum of the two numbers and determine if it is divisible by 5.

public class LabExercise39 extends javax.swing.JFrame {

    public LabExercise39() {
        initComponents();
    }

    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Enter second number:");

        jLabel1.setText("Enter first number:");

        jButton1.setText("ADD");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jLabel4.setText("Result:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                                    .addComponent(jTextField2))
                                .addGap(18, 18, 18)
                                .addComponent(jButton1))
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jLabel3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
         
        float sum, num1, num2;
       
        num1 = Float.parseFloat(jTextField1.getText());
        num2 = Float.parseFloat(jTextField2.getText());
       
        sum = num1 +num2;
       
        if(sum%5==0){
        jTextField3.setText(String.valueOf(sum+ " and it is divisible by 5."));
        }else jTextField3.setText(String.valueOf(sum));
       
}

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

}

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {

    }

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LabExercise39().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;

}

Water District

Assume you are an employee from Davao City Water District. You are then asked to develop a program that will accept bill payments in the company to cater more customer in a day faster.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LabExercise44 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LabExercise44 frame = new LabExercise44();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LabExercise44() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 400, 230);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        JLabel lblNewLabel = new JLabel("Davao City Water District");
        lblNewLabel.setBounds(10, 11, 146, 14);
        contentPane.add(lblNewLabel);
       
        JLabel lblEnterYourName = new JLabel("Enter Your Name:");
        lblEnterYourName.setBounds(10, 50, 126, 14);
        contentPane.add(lblEnterYourName);
       
        JLabel lblEnterYourPayment = new JLabel("Enter Your Payment:");
        lblEnterYourPayment.setBounds(10, 75, 126, 14);
        contentPane.add(lblEnterYourPayment);
       
        textField = new JTextField();
        textField.setBounds(146, 47, 129, 20);
        contentPane.add(textField);
        textField.setColumns(10);
       
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(146, 72, 129, 20);
        contentPane.add(textField_1);
       
        JButton btnPay = new JButton("Pay");
        btnPay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                textField.getText();
                textField_1.getText();
                textField_2.setText("Name:\t"+textField.getText());
                textField_3.setText("Paid:\t"+textField_1.getText());
                textField_4.setText("Thank you for paying!");
               
               
            }
        });
        btnPay.setBounds(285, 46, 89, 23);
        contentPane.add(btnPay);
       
        JButton button = new JButton("Clear");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                textField.setText("");
                textField_1.setText("");
                textField_2.setText("");
                textField_3.setText("");
                textField_4.setText("");
               
            }
        });
        button.setBounds(285, 71, 89, 23);
        contentPane.add(button);
       
        textField_2 = new JTextField();
        textField_2.setBounds(79, 122, 222, 20);
        contentPane.add(textField_2);
        textField_2.setColumns(10);
       
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(79, 142, 222, 20);
        contentPane.add(textField_3);
       
        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(79, 161, 222, 20);
        contentPane.add(textField_4);
       
    }
}

PowerBase

Power and Base program. Create Class named powerbase which has a method definition ‘public static double power(double base, double power)’ that will return the value equals the base, which is the number n times by the power. Instantiate your class and use them in your main program.

Sample/Input:
Enter two numbers: 2 3
The result is 8.

import javax.swing.JOptionPane;
public class PowerBase {

    public static double Power(double base, double power){
       
        double total = 1.0;
        for(int i = 0;i<power;i++){
            total = base * total;
        }
        return total;
    }
    public static void main(String[] args) {
       
        PowerBase power = new PowerBase();
       
        double result = 0.0;
       
        result = power.Power(2, 3);

        JOptionPane.showMessageDialog(null, result);
    }
}

Display 1-100

Create a program that will display all numbers from 1 to 100.

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class LabExercise47 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LabExercise47 frame = new LabExercise47();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LabExercise47() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 500, 220);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
       
        JButton btnNewButton = new JButton("Display 1-100");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               
                String h = "";
                String j = "";
                String a = "";
                String b = "";
                for(int i = 1;i<=25;i++){
                   
                    h = h + i +" ";
                   
                }
               
                for(int i = 26;i<=50;i++){
                   
                    j = j + i +" ";
                   
                }
                for(int i = 51;i<=75;i++){
                   
                    a = a + i +" ";
                   
                }
               
                for(int i = 76;i<=100;i++){
                   
                    b = b + i +" ";
                   
                }
                textField.setText(String.valueOf(h));
                textField_1.setText(String.valueOf(j));
                textField_2.setText(String.valueOf(a));
                textField_3.setText(String.valueOf(b));
            }
        });
        btnNewButton.setBounds(5, 5, 110, 23);
        contentPane.add(btnNewButton);
       
        textField = new JTextField();
        textField.setBounds(5, 39, 471, 35);
        contentPane.add(textField);
        textField.setColumns(10);
       
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(5, 73, 471, 35);
        contentPane.add(textField_1);
       
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(5, 107, 471, 35);
        contentPane.add(textField_2);
       
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(5, 141, 471, 35);
        contentPane.add(textField_3);
     }
}

Sum first n positive odd integers

 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 LabExercise13 {
   
    public static void main(String[] args) {
        String b = "";
        int a = 0;
        int n;
        int count=0;
       
        n = Short.parseShort(JOptionPane.showInputDialog("Enter an integer: "));
       
        for (int i=0;count<n;i++){
                if(i%2!=0){
                    b = b + " " + i;
                    a+=i;
                    count++;
                }        
        }
        JOptionPane.showMessageDialog(null,"Sum of: "+b +"\nis: " +a);
    }
}

Multiplication Table

Program to display multiplication tables from 1 to 10 using 2d array.
___________________________________________________

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class LabExercise26 {

    public static void main(String[] args) {
        int x[][] = new int[11][11];
           
            String all = "";
           
            for(int i = 1;i<=10;i++){
                for(int j = 1;j<=10;j++){
                    x[i][j]= i*j;
                    all = all + x[i][j] + "\t";
                }
                all = all + "\n\n";
            }
            JOptionPane.showMessageDialog(null, new JTextArea(all));    
    }
}

Names

Write a Java program for sorting a given list of names in ascending order.
___________________________________________________________

import javax.swing.JOptionPane;
import java.util.Arrays;
public class LabExercise27 {

    public static void main(String[] args) {

        String x[] = new String[5];
       
        x[0] = "Jack";
        x[1] = "Romeo";
        x[2] = "Joyce";
        x[3] = "Keith";
        x[4] = "Elieza";
       
        String all = "";
        String holder = "";
        int j = 0;
        int h = 0;
       
        Arrays.sort(x);
       
        for(int i = 0; i<5; i++){
            all = all +" "+ x[i];
        }
        JOptionPane.showMessageDialog(null, "Ascending order: "+all);
    }
}

Basic GUI

Design the following GUI in Java using JFrame and other components
______________________________________________________________________
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class LabExercise38 extends javax.swing.JFrame {

 
    public LabExercise38() {
        initComponents();
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Enter first number      :");

        jLabel2.setText("Enter second number :");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField2)))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jButton1)))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   
    }

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {

    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LabExercise38().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;

}

Reversed String


Write a loop that will create a new string that is the reverse of a given string.
________________________________________________________

import javax.swing.JOptionPane;

public class LabExercise16 {
    public static void main(String[] args) {
       
        String str = "";
        String str2="";
       
        str = JOptionPane.showInputDialog("Enter String: ");
        str = str.toLowerCase();

        str2 = new StringBuffer(str).reverse().toString();
       
        JOptionPane.showMessageDialog(null, "Original String: "+str+"\nReversed String: "+str2);
    }
}

Sum first n positive odd integers (Square)

Write a for statement to compute the sum 1 + 2² + 3² + 4² + 5² + ... + n².
import javax.swing.JOptionPane;
public class LabExercise14 {

    public static void main(String[] args) {
        String b = "";
        int a = 0;
        int n;
        int count=0;
       
        n = Short.parseShort(JOptionPane.showInputDialog("Enter an integer: "));
       
        for (int i=0;count<n;i++){
                if(i%2!=0){
                    b = b + " " + i;
                    a+=i*i;
                    count++;
                }               
        }
        JOptionPane.showMessageDialog(null,"Sum(square) of: "+b +"\nis: " +a);
    }
}

CurrencyConverter

Class Name: CurrencyConverter. The four major currencies USD (US Dollar), GBP (Great British Pound), EUR (Euro), and JPY (Japanese Yen) has its following exchange rates against a peso respectively: USD=43.75, GBP=78.98, EUR=56.45, and JPY=0.867. Develop a program that will compute how much will it costs in peso for every given currency. Calculate and display the total amount. (Ask 4 inputs for each of the currency, afterwards, display its equivalent peso exchange rate.

import java.util.Scanner;
public class CurrencyConverter {
 public static void main (String[]args){
     Scanner input = new Scanner(System.in);
   
     double peso1, peso2, peso3, peso4, total;
     double USD = 43.75;
     double GBP = 78.98;
     double EUR = 56.45;
     double JPY = 0.867;
     System.out.print("Enter amount of USD: ");
     int usd = input.nextInt();
     System.out.print("Enter amount of GBP: ");
     int gbp = input.nextInt();
     System.out.print("Enter amount of EUR: ");
     int eur = input.nextInt();
     System.out.print("Enter amount of JPY: ");
     int jpy = input.nextInt();
       
     peso1 = usd * USD;
     peso2 = gbp * GBP;
     peso3 = eur * EUR;
     peso4 = jpy * JPY;
     total = peso1+peso2+peso3+peso4;
   
     System.out.println("To PHP:");
     System.out.println("USD: "+peso1);
     System.out.println("GBP: "+peso2);
     System.out.println("EUR: "+peso3);
     System.out.println("JPY: "+peso4);
     System.out.println("Total PHP: "+total);

 }
}

Quadratic

Class Name: Quadratic. Create a program to find the roots of a quadratic equation using the formula, d=b2-4*a*c. Note that for any negative value in d, there is no root available. If value is equal to 0, roots are equal. Use the formula r=-b/(2*a) in getting the roots. Otherwise, real roots can be derived by getting the square root of d, then use this r=-b+d/(2*a). Then, Display the roots.

______________________________________________________________

import java.util.Scanner;
public class Quadratic {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
       
        double a=0;
        double b=0;
        double c=0;
        double d=0;
        double r=0;
        double r2=0;
       
        System.out.print("This program finds the roots of a quadratic equation.");
        System.out.print("\nEnter first value [a]: ");
        a=input.nextDouble();
        System.out.print("Enter second value [b]: ");
        b=input.nextDouble();
        System.out.print("Enter first value [c]: ");
        c=input.nextDouble();
       
        d=(b*b)-4*(a*c);
        r=b/(2*a);
        r2=-b+d/(2*a);
       
        if (d<0){System.out.print("No roots.");}
        else if (d==0){System.out.print("Roots are equal: "+r);}
        else System.out.print("Roots: "+r+ " & "+r2);
    }
}

NameValidation

Class Name: NameValidation. Write a fragment of code that will read words from the keyboard until the word exit is entered. For each word except exit, report whether its first character is equal to its last character.
______________________________________________________________

import java.util.*;
public class NameValidation {

    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
       
        boolean done = false;
        String str = "exit";
       
       while (!done){
        System.out.print("Enter a word: \t");
        String str2 = input.next();
        str2 = str2.toLowerCase();
       
            int number = str2.length();
            int number1 = 0;
           
            String x = str2.substring(number1,1);
            String y = str2.substring((number - 1), number);
           
            System.out.println();
            if (str.equals(str2)) {
                System.out.println("Program is now terminating...");
                done = true;
            }
            else {
                done = false; 
               
            if (x.equals(y)){
                System.out.println("The first character is equal to the last character: " + str2);
            }
            else {
                System.out.println("The first character is not equal to the last character: " + str2);
            }   
            }
        }
    }
}

Beverage

Suppose that you work for a beverage company. The company wants to know the optimal cost for a cylindrical container that holds a specified volume. Write a fragment of code that uses an ask-before-iterating loop. During each iteration of the loop, your code will ask the user to enter the volume and the radius of the cylinder. Compute and display the height and cost of the container. Use the following formulas, where V is the volume, r is the radius, h is the height, and C is the cost.
h = V/πr²
C = 2πr(r + h)

__________________________________________________________________


import javax.swing.JOptionPane;
public class LabExercise19 {

    public static void main(String[] args) {

        Double V = 0.0;
        Double pie = 3.14;
        Double r = 0.0;
        Double C = 0.0;
        Double h = 0.0;
        String choice = "";
       
        do{
            V = Double.parseDouble(JOptionPane.showInputDialog("Enter volume: "));
            r = Double.parseDouble(JOptionPane.showInputDialog("Enter radius: "));
       
            h = V/(pie*(r*r));
            C = 2*pie*r*(r+h);
           
            JOptionPane.showMessageDialog(null, "Height: "+h+"\nCost: "+C);
           
            choice = JOptionPane.showInputDialog("Another one? Y/N");
            choice = choice.toLowerCase();
        }while(!choice.equals("n"));
    }

}

advertisement