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);
}
}
}
}
}
Sunday, August 24, 2014
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"));
}
}
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");
}
}
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.
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;
}
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;
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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.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);
}
}
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);
}
}
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);
}
}
}
}
}
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)
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"));
}
}
Sunday, August 17, 2014
History of the Java™ programming language
On 23 May 1995, John Gage, the director of the Science Office of the Sun Microsystems along with Marc Andreesen, co-founder and executive vice president at Netscape announced to an audience of SunWorldTM that Java technology wasn't a myth and that it was a reality and that it was going to be incorporated into Netscape Navigator.
At the time the total number of people working on Java was less than 30. This team would shape the future in the next decade and no one had any idea as to what was in store. From being the mind of an unmanned vehicle on Mars to the operating environment on most of the consumer electronics, e.g. cable set-top boxes, VCRs, toasters and also for personal digital assistants (PDAs). Java has come a long way from its inception.
Can you consider a graduate of a 3-month Computer Technician Course a professional? Justify your answer.
Answering this question could make a forum if someone can read my
opinion but nonetheless, I'll still answer this. ('coz required. haha
:D)
For me, a 3-month graduate of Computer Technician Course is not
considered a professional. First of all, because "3 months" is so short
compared to "4 years". Lets just say that you learned almost everything
in 3 months, but I'm sure you'll gain much more knowledge for 4 years.
When you just took up a 3-month course, you need have a lot of
experiences just to prove that you have that "thing" that the company
needs but when you took up a 4-year course (surely you've gain so much
experiences in that span of time), the company will have a less doubt
about you.
When can you say whether a person is an IT Professional or not?
My first post was in general. Now in this portion, it would be more specific.
IT Professional or Information Technology Professional.
Being an IT known to be good about computers. It could be in programming, web designer and many more. So if we say that a person is an IT professional, he/she is really good at computer related things. He/she knows how to handle problems related to computer. Well informed about the latest trending or facts roaming around the world wide web. When in terms of logical thinking, he/she is good at it.
Well, when we say that a person is not an IT professional, we could just say that he/she is the opposite of what I've said on the upper part. :D
What is a professional?
For a denotative meaning:
1. Expert and specialized knowledge in field which one is practising professionally.
2. Excellent manual/practical and literary skills in relation to profession.
For my own idea, being professional can be described as a person which is well-oriented or well-educated on what is he/she doing. He/she can easily answer questions regarding to his/her work. You can easily trust him/her any task that is related to his/her profession. :D
Subscribe to:
Posts (Atom)