Exer5—Greeting—
/*Programmer: Ana Mae B. Respecia
Date started: March 14, 2009
Date ended: March 18, 2009
Description: This program inputs user’s greetings and prints out the greeting.*/
import java.util.Scanner;
public class Greetings {
public static void main(String[] args) {
Scanner a= new Scanner(System.in);
String mygreet;
System.out.println("Enter your Greetings:");// prompts user to enter greetings
mygreet= a. nextLine();
System.out.println(" ");
System.out.println(" "+mygreet);// prints ouy the greetings
}
}
Exer2—Color Cycle—
/*Programmer: Ana Mae B. Respecia
Date started: March 13, 2009
Date ended: March 18, 2009
Description: Program that when you click 1 button the color will interchange to green, blue, gray, red */
import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import java.awt.BorderLayout;import java.awt.Color;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Button extends JPanel implements ActionListener {private static boolean USE_CROSS_PLATFORM_UI = false;int buttonLabelIndex = 0;String buttonLabels[] = { "Green", "blue", "Gray", "Red" };Color buttonColors[] = { Color.GREEN, Color.BLUE, Color.GRAY, Color.RED,};JButton button;public Button() {super(new BorderLayout());button = new JButton(buttonLabels[buttonLabelIndex]);if(USE_CROSS_PLATFORM_UI) {button.setBackground(buttonColors[buttonLabelIndex]);} else {button.setForeground(buttonColors[buttonLabelIndex]);}button.addActionListener(this);this.add(button, BorderLayout.CENTER);this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));}public void actionPerformed(ActionEvent e) {buttonLabelIndex = ++buttonLabelIndex < buttonLabels.length?buttonLabelIndex:0;button.setText(buttonLabels[buttonLabelIndex]);this.setBackground(buttonColors[buttonLabelIndex]);button.setBackground(buttonColors[buttonLabelIndex]);}private static void run() {if(USE_CROSS_PLATFORM_UI) {try {UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());} catch (Exception e) {e.printStackTrace();}}JFrame frame = new JFrame("Button Demo");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JComponent contentPane = new Button();contentPane.setOpaque(true);frame.setContentPane(contentPane);frame.pack();frame.setVisible(true);}public static void main(String[] args) {run();}}Exer3 – Combination Lock—
/*Programmer: Ana Mae B. Respecia
Date started: March 14, 2009
Date ended: March 18, 2009
Description: this program is labeled 0-9 and if the user enters the combination 7-3-5 the program will exit otherwise it will turn to red.*/
public class combi extends JFrame{ String[] password={"8","4","9"}; int z=0,a=0;
JButton buttons[];
JPanel buttonPanel;
Container container = getContentPane();
public combi(){
super("Combination Lock");
ClickEvent action = new ClickEvent();
container.setLayout(new FlowLayout());
buttons = new JButton[10];
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(0,buttons.length));
public CombinationLock () {
Container ana = getContentPane();
ana.setLayout(new FlowLayout());
ana.add(new JButton("0"));
ana.add(new JButton("1"));
ana.add(new JButton("2"));
ana.add(new JButton("3"));
ana.add(new JButton("4"));
ana.add(new JButton("5"));
ana.add(new JButton("6"));
ana.add(new JButton("7"));
ana.add(new JButton("8"));
ana.add(new JButton("9"));
ana.setBackground(gray);
pack();
}
}
Exer4—Name Echo—
/*Programmer: Ana Mae B. Respecia
Date started: March 14, 2009
Date ended: March 18, 2009
Description: This program inputs user’s name and will turn the second name into Uppercase letter .*/
import java.util.*;import java.io.*;
public class Upper{
public static void main(String[] args)throws StringIndexOutOfBoundsException{
try{
public String wafa;
Scanner ana = new Scanner(System.in);
//ask the user to input some text
System.out.println("Enter your name:");
//get the data unput by the uset and store it in variable ana.
String ana= wafa.nextLine();
//get the " " through indexOf method
int c=name.indexOf(" ");
int a=c+1;
//store the first name entered by the user to index output
String output = name.substring(0,c);
//store the second name entered to index name2
String name2=name.substring(a,name.length());
//print the first name and the second name in capital letters
System.out.print(output+" "+name2.toUpperCase());
}
catch(StringIndexOutOfBoundsException index){
System.err.printf("No more data.");
}
}
}
Exer 1—Word Reverser--
/*Programmer: Ana Mae B. Respecia
Date started: March 13, 2009
Date ended: March 18, 2009
Description: Program that reads sentences from the user and prints out each word reversed.*/
import java.util.*;
public class WordReverser{
public static void main(String args[]){
String reverse;
String ana;
Scanner scan=new Scanner(System.in);
System.out.println("Input:");
reverse=scan.nextLine();// input user to enter the desired word
StringTokenizer st=new StringTokenizer(ana) ;
while (st.hasMoreTokens()) {
ana=st.nextToken();// reverse and read each word
ana=new StringBuffer(ana).reverse().toString();// pass to so that the word will be reverse
System.out.print(ana+" ");// prints out the reversed output
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment