 |

 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/*
* Program Name: Main.java
* Author: Sue Feng
* Date Created: November 10, 2007, 3:01 PM
* Purpose: Create a program that plays a game of red, white and blue.
* On a 6 X 6 grid, a player and a (very illogical) computer play to
* see who can get four colors in a row first.
* How to test this program: Enter an x coordinate when it says "x coordinate
* between 0-5:", and a y coordinate when it says "y coordinate between 0-5:"
* Be sure to try numbers greater than five and less than zero. It should then
* instigate the loop that says "Please enter an integer between 0 and 5" if you
* enter numbers less than zero or greater than five, and you'll have to re-enter
* coordinates until you pick numbers between 0 and 5. Try getting four in a row,
* or four in a column. If you get four in a row or four in a column, it should
* say "You won!!!", or if the computer wins it should say "The computer won!!!".
* Also, if the board is full or if someone chooses a coordinate that is already
* taken, it should say "Please choose different coordinates. The coordinates you
* selected were already taken. If the board is full, nobody wins. The game is over."
*/
package fengsuehwtwo;
import java.util.Scanner;
import java.util.Random;
/**
*
* @author Sue
*/
public class Main {
private static int x, y;
// create a two dimensional array
private static char[] randomChar = new char[]{'R','W','B'};
private static int numRows = 7;
private static int numCols = 7;
private static char chipColor;
private static char [][] characters = {{' ' , '0', '1', '2', '3', '4', '5'},
{'0', '-', '-', '-', '-', '-', '-'},
{'1', '-', '-', '-', '-', '-', '-'},
{'2', '-', '-', '-', '-', '-', '-'},
{'3', '-', '-', '-', '-', '-', '-'},
{'4', '-', '-', '-', '-', '-', '-'},
{'5', '-', '-', '-', '-', '-', '-'}};
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("--------------------------------------------------------------------------------------\n" +
"|~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~* Connect Four *~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n" +
"| Instructions: This is a simple four in a row or four in a column game. |\n" +
"| The computer generates random chip colors such as R=Red, B=Blue, W=White. Choose x |\n" +
"| and y coordinates for where you want to place your chip. Then the computer goes. |\n" +
"| Whoever is able to make four in a row or four in a column wins. If the board is |\n" +
"| full and there are no four in a row or four in a column, nobody wins and the game |\n" +
"| is over. |\n" +
"--------------------------------------------------------------------------------------");
// The outer for loop sets up the row numbers.
printCharacters();
while ((isFourInARow()==false)&&(isFourInACol()==false)) {
Random gen = new Random();
int r = gen.nextInt(randomChar.length);
chipColor= randomChar[r];
// player input
System.out.println("Chip color is " + chipColor);
Scanner scan = new Scanner(System.in);
System.out.println("Where do you want to put the chip?");
System.out.println("x coordinate between 0-5: ");
x = scan.nextInt();
while ((x>5)||(x<0)){
System.out.println("Please enter an integer between 0 and 5.");
System.out.println("x coordinate between 0-5: ");
x = scan.nextInt();
}
System.out.println("y coordinate between 0-5: ");
y = scan.nextInt();
while ((y>5)||(y<0)){
System.out.println("Please enter an integer between 0 and 5.");
System.out.println("y coordinate between 0-5: ");
y = scan.nextInt();
}
while ((characters[x+1][y+1]=='B')||(characters[x+1][y+1]=='R')||(characters[x+1][y+1]=='W')){
System.out.println("Please choose different coordinates. The coordinates you selected were already taken.");
System.out.println("If the board is full, nobody wins. The game is over.");
System.out.println("x coordinate between 0-5: ");
x = scan.nextInt();
System.out.println("y coordinate between 0-5: ");
y = scan.nextInt();
}
characters[x+1][y+1]=chipColor;
printCharacters();
if ((isFourInARow()==true)||(isFourInACol()==true)){
System.out.println("You Won!!!");
break;
}
// computer generated
if((isFourInARow()==false)&&(isFourInACol()==false)) {
while ((characters[x+1][y+1]=='B')||(characters[x+1][y+1]=='R')||(characters[x+1][y+1]=='W')){
x = gen.nextInt(6);
y = gen.nextInt(6);
}
System.out.println("The computer chooses to place the " + chipColor + " chip at coordinate " + x + ", " + y);
characters[x+1][y+1]=chipColor;
printCharacters();
}
if((isFourInARow()==true)||(isFourInACol()==true)) {
System.out.println("The computer won!!!");
break;
}
}
// end while
}
public static void printCharacters() {
// The outer for loop sets up the row numbers
for (int i = 0; i < numRows; i++){
// The inner for loop sets up the column numbers.
for (int j = 0; j < numCols; j++) {
System.out.print(characters[i][j] + " ") ;
}
System.out.println() ;
}
}
// Method: find if there are four in a row
public static boolean isFourInARow() {
for(int i =0; i < numRows; i++) {
char temp;
for(int j=0; j < numCols-3; j++) {
temp = characters[i][j];
if (temp!='-') {
if ((temp == characters[i][j+1]) && (temp == characters[i][j+2]) && (temp == characters[i][j+3])) {
return true;
}
}
}
}
return false;
}
// Method: find if there are four in a column
public static boolean isFourInACol() {
for(int i =0; i < numRows-3; i++) {
char temp;
for(int j=0; j < numCols; j++) {
temp = characters[i][j];
if (temp!='-') {
if ((temp == characters[i+1][j]) && (temp == characters[i+2][j]) && (temp == characters[i+3][j])) {
return true;
}
}
}
}
return false;
}
}
Tags: cs 106 homework
|
 |
 |
 |
 |
|
 |
 |

 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/*
* Main.java
* Author: Sue Feng
* Created on November 15, 2007, 2:33 PM
*
* Purpose: In this lab, you’ll create a program that contains two (or more)
* numbers, and add the numbers together. Then you’ll display the results
* on a window. After doing this, you will modify the program.
*/
package fengsuelabeleven;
import javax.swing.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame ("Sights and Sounds Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GraphicsFun());
frame.pack();
frame.setVisible(true);
}
}
/*
* GraphicsFun.java
*
* Created on November 15, 2007, 2:35 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package fengsuelabeleven;
import java.awt.*;
import javax.swing.*;
/**
*
* @author sfeng
*/
public class GraphicsFun extends JPanel {
private JPanel mainPanel;
private JLabel label1, label2, label3, label4;
private JPanel panel1, panel2, panel3, panel4;
private JPanel num1, num2;
/** Creates a new instance of GraphicsFun */
public GraphicsFun() {
int num1 = 5, num2 = 10;
int sum = num1 + num2;
int diff = num1 - num2;
int product = num1 * num2;
float quotient = (float)num1 / num2;
// Create a panel and a label
mainPanel = new JPanel();
mainPanel.setBackground(Color.yellow);
mainPanel.setPreferredSize(new Dimension(360,360));
panel1 = new JPanel();
panel1.setBackground(Color.orange);
panel1.setPreferredSize(new Dimension(360,60));
panel2 = new JPanel();
panel2.setBackground(Color.darkGray);
panel2.setPreferredSize(new Dimension(360,60));
panel3 = new JPanel();
panel3.setBackground(Color.lightGray);
panel3.setPreferredSize(new Dimension(360,60));
panel4 = new JPanel();
panel4.setBackground(Color.white);
panel4.setPreferredSize(new Dimension(360,60));
label1 = new JLabel(num1 + " + " + num2 + " = " + sum);
label1.setForeground(Color.blue);
label2 = new JLabel(num1 + " - " + num2 + " = " + diff);
label2.setForeground(Color.cyan);
label3 = new JLabel(num1 + " * " + num2 + " = " + product);
label3.setForeground(Color.red);
label4 = new JLabel(num1 + " / " + num2 + " = " + quotient);
label4.setForeground(Color.black);
// Add the label to the panel
panel1.add(label1);
panel2.add(label2);
panel3.add(label3);
panel4.add(label4);
mainPanel.add(panel1);
mainPanel.add(panel2);
mainPanel.add(panel3);
mainPanel.add(panel4);
// Add the panel to the frame
add(mainPanel);
}
}
Tags: cs 106 lab
|
 |
 |
 |
 |
|
 |
 |


 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/*
* Calculate.java
*
* Created on November 14, 2007, 7:09 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package nov14calculator;
import javax.swing.*;
import java.awt.*;
/**
*
* @author sfeng
*/
public class Calculate extends JPanel{
private JPanel mainPanel, subPanel1, subPanel2, subPanel3, subPanel4, subPanel5;
private JButton button0, button1, button2, button3, button4,
button5, button6, button7, button8, button9;
private JButton equals, plus, minus, times, divide;
private JLabel label;
private JTextField result;
/** Creates a new instance of Calculate */
public Calculate() {
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300, 300));
// Set up the first subpanel
subPanel1 = new JPanel();
subPanel1.setPreferredSize(new Dimension(300, 50));
subPanel1.setBackground(Color.white);
// Set up the second subpanel
subPanel2 = new JPanel();
subPanel2.setPreferredSize(new Dimension(300, 50));
subPanel2.setBackground(Color.white);
// Set up the third subpanel
subPanel3 = new JPanel();
subPanel3.setPreferredSize(new Dimension(300, 50));
subPanel3.setBackground(Color.white);
// Set up the fourth subpanel
subPanel4 = new JPanel();
subPanel4.setPreferredSize(new Dimension(300, 50));
subPanel4.setBackground(Color.white);
subPanel5 = new JPanel();
subPanel5.setPreferredSize(new Dimension(300, 50));
subPanel5.setBackground(Color.WHITE);
// Set up label
label = new JLabel("Result:");
label.setForeground(Color.black);
label.setPreferredSize(new Dimension(50, 50));
// Set up the textfield
result = new JTextField();
result.setPreferredSize(new Dimension(100, 30));
// add button to the subpanel4
button0 = new JButton("0");
equals = new JButton("=");
plus = new JButton("+");
subPanel4.add(button0);
subPanel4.add(equals);
subPanel4.add(plus);
// add buttons to the subpanel3
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
minus = new JButton("-");
subPanel3.add(button1);
subPanel3.add(button2);
subPanel3.add(button3);
subPanel3.add(minus);
// add buttons to the subpanel2
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
times = new JButton("*");
subPanel2.add(button4);
subPanel2.add(button5);
subPanel2.add(button6);
subPanel2.add(times);
// add buttons to the subpanel1
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
divide = new JButton("/");
subPanel1.add(button7);
subPanel1.add(button8);
subPanel1.add(button9);
subPanel1.add(divide);
subPanel5.add(label);
subPanel5.add(result);
mainPanel.add(subPanel1);
mainPanel.add(subPanel2);
mainPanel.add(subPanel3);
mainPanel.add(subPanel4);
mainPanel.add(subPanel5);
// add mainPanel to the window
add(mainPanel);
}
}
/*
* Main.java
*
* Created on November 14, 2007, 7:08 PM
*
* Purpose: creates a calculator
*/
package nov14calculator;
import javax.swing.*;
/**
*
* @author sfeng
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Calculate());
// pack method sizes the frame so that all its contents are
// at or above their preferred size (java sun documentation)
frame.pack();
frame.setVisible(true);
}
}
Tags: cs 106 code
|
 |
 |
 |
 |
|
 |
 |

 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/* * Applications.java * * Created on November 14, 2007, 6:44 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package nov14example3; import javax.swing.*; import java.awt.*; /** * * @author sfeng */ public class Applications extends JPanel{ private JPanel mainPanel, subPanel1, subPanel2; private JLabel label1, label2; private JTextField name; private JButton submitButton; public Applications() { // create the primary Panel mainPanel = new JPanel(); mainPanel.setPreferredSize(new Dimension(170, 325)); // Set up the first subpanel subPanel1 = new JPanel(); subPanel1.setPreferredSize(new Dimension(150, 100)); subPanel1.setBackground(Color.GREEN); // Set up the second subpanel subPanel2 = new JPanel(); subPanel2.setPreferredSize(new Dimension(150, 100)); subPanel2.setBackground(Color.RED); // set up the label label1 = new JLabel("Name:"); label1.setForeground(Color.RED); label1.setPreferredSize(new Dimension(40, 15)); // Set up the textfield name = new JTextField(); name.setPreferredSize(new Dimension(100, 15)); // add button to the subpanel2 submitButton = new JButton("Submit"); subPanel2.add(submitButton); subPanel1.add(label1); subPanel1.add(name); mainPanel.add(subPanel1); mainPanel.add(subPanel2); // add mainPanel to the window add(mainPanel); } } Tags: cs106 code
|
 |
 |
 |
 |
|
 |
 |


 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/*
* NestedPanels.java
*
* Created on November 14, 2007, 6:26 PM
*
* Purpose: Demonstrate how to put panels inside other panels.
*/
package nov14example2;
import javax.swing.*;
import java.awt.*;
/**
*
* @author sfeng
*/
public class NestedPanels extends JPanel{
private JPanel mainPanel, subPanel1, subPanel2, subPanel3;
private JLabel label1, label2, label3;
/** Creates a new instance of NestedPanels */
public NestedPanels() {
// create the primary Panel
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(170, 325));
// Set up the first subpanel
subPanel1 = new JPanel();
subPanel1.setPreferredSize(new Dimension(150, 100));
subPanel1.setBackground(Color.GREEN);
// Set up the second subpanel
subPanel2 = new JPanel();
subPanel2.setPreferredSize(new Dimension(150, 100));
subPanel2.setBackground(Color.RED);
// Set up the second subpanel
subPanel3 = new JPanel();
subPanel3.setPreferredSize(new Dimension(150, 100));
subPanel3.setBackground(Color.BLUE);
// set up the label
label1 = new JLabel("One");
subPanel1.add(label1);
label2 = new JLabel("Two");
subPanel2.add(label2);
label3 = new JLabel("Three");
subPanel3.add(label3);
// add the subpanel to the mainpanel
mainPanel.add(subPanel1);
mainPanel.add(subPanel2);
mainPanel.add(subPanel3);
// add mainPanel to the window
add(mainPanel);
}
}
/*
* Main.java
*
* Created on November 14, 2007, 6:23 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package nov14example2;
import javax.swing.*;
/**
*
* @author sfeng
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame frame = new JFrame("Example 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new NestedPanels());
// pack method sizes the frame so that all its contents are
// at or above their preferred size (java sun documentation)
frame.pack();
frame.setVisible(true);
}
}
Tags: cs 106 code
|
 |
 |
 |
 |
|
 |
 |




 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/*
* Main.java
* Author: Sue Feng
* Created on November 9, 2007, 4:59 PM
*
* Purpose: The purpose fo this lab is to get practice with two dimentional
* arrays. There will be manipulations of an array that contains hardcoded
* nummbers, for instance, adding five to every number in a row.
*/
package fengsuepostlabten;
/**
*
* @author Sue
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
printTotal();
printAverage();
// Prints totals for the 3rd row
printRowTotals(3);
// Prints totals for the 5th row
printRowTotals(5);
// Prints element in 3rd row, 5th column
printOneItem(3, 5);
// Prints element in the 2nd row, 1st column
printOneItem(2, 1);
}
// declare row and column length
private static float numRows = 5f;
private static float numColumns = 5f;
// declare numbers as a two-dimensional array of
// type int
private static int [][] numbers = { {1, 2, 3, 4, 5},
{10, 20, 30, 40, 50},
{100, 200, 300, 400, 500},
{10, 20, 30, 40, 50},
{1, 2, 3, 4, 5}};
// This prints the total of all numbers in the array added together.
static int total = 0;
static int i = 0;
static int j = 0;
public static void printTotal(){
for (i = 0; i < numRows; i++){
for (j = 0; j < numColumns; j++) {
total += numbers[i][j];
}
}
System.out.println("Total: " + total);
}
// This prints the total of all numbers in the array added together.
public static void printAverage(){
float avg = total/(numRows*numColumns);
System.out.println("Average: " + avg);
}
// This method has one parameter, an integer representing the row to be added and printed.
public static void printRowTotals(int addRow){
int rowTotal = 0;
for(j=0; j< numColumns; j++) {
rowTotal += numbers[addRow-1][j];
}
System.out.println("Average for row " +addRow + ": "+ rowTotal);
}
//This method has two integer parameters, the row and column of the element to be printed.
public static void printOneItem(int addedRow, int addedColumn){
System.out.println("Value for " + addedRow + ", " + addedColumn + " is " + numbers[addedRow-1][addedColumn-1]);
}
}
Tags: cs 106 lab
|
 |
 |
 |
 |
|
 |
 |

 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
/*
* Main.java
* Author: Sue Feng
* Created on November 8, 2007, 1:22 PM
*
* Purpose: This particular problem sets up a key with the 5 correct answers
* (A-D). The user will enter the student’s answers, and as each answer is
* reviewed against the key, the program will keep track of the total number
* of correct answers. It will also display the percentage of answers the user
* got correct and whether the student passed or failed.
*/
package fengsuelabten;
import java.util.Scanner;
/**
*
* @author Sue
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
giveQuiz();
twoDExample();
}
public static void giveQuiz() {
//a variable that provides the number of questions to be asked
final int numQuestions = 5;
//two arrays, one for studentAnswers (type char) and one for answerKey (also type char).
//Note that the actual values of the answerKey are hardcoded into the array.
//(answerKey[0] = ‘A’, answerKey[1] = ‘D’, etc.)
char[] studentAnswers = new char[numQuestions+1];
char[] answerKey = { 'A', 'D', 'B', 'C', 'A' };
// answerKey[0] holds the value of the correct answer for problem #1. Note that there is a one-number offset.
int problemNum = 1;
Scanner scan = new Scanner(System.in);
int correctAnswer = 0;
while(problemNum <=numQuestions) {
// Identify the problem number that the program wants the answer for
System.out.println("Please enter the student's answer for the problem number " + problemNum);
// Get the student’s answer and store it in the appropriate arrray
studentAnswers[problemNum]=scan.next().charAt(0);
// Compare the student’s answer to the correct answer and keep a
// running total of how many questions the student answered
// correctly
if (studentAnswers[problemNum]==answerKey[problemNum-1]) {
correctAnswer++;
}
problemNum++;
} // end while
// Print the total correct and the total incorrect
System.out.println("Total correct answers: " + correctAnswer);
System.out.println("Total incorrect answers: " + (numQuestions-correctAnswer));
// Print the percent grade
float percentGrade = (correctAnswer/5f) * 100f;
System.out.println("Percent grade: " + percentGrade + "%");
// Print whether the student passed or failed (60% or higher is pass)
if (percentGrade>=60){
System.out.println("Congratulations! You passed the test!");
} else
System.out.println("Sorry but you did not pass the test.");
} // end givenQuiz method
public static void twoDExample(){
final int numRows = 5;
final int numCols = 5;
int[][] someNums = { { 27, 63, 21, 45, 21},
{49, 56, 1, 91, 100},
{50, 44, 31, 28, 71},
{51, 23, 76, 31, 32},
{52, 66, 88, 77, 33} };
// The outer for loop sets up the row numbers.
for (int x=0; x < numRows; x++){
// The inner for loop sets up the column numbers.
for (int y = 0; y < numCols; y++) {
System.out.print(someNums[x][y] + "\t") ;
}
System.out.println() ;
}
// Print the following numbers from the array: 1, 100, 23, 77
System.out.println(someNums[1][2]);
System.out.println(someNums[1][4]);
System.out.println(someNums[3][1]);
System.out.println(someNums[4][3]);
for(int i=0; i<=someNums.length-1; i++) {
// add 5 to all numbers in the 4th row
someNums[3][i] = someNums[3][i]+5;
System.out.println(someNums[3][i]);
}
// Add 10 to the following numbers in the array: 1 and 100
someNums[1][2] = someNums[1][2]+10;
someNums[1][4] = someNums[1][4]+10;
System.out.println(someNums[1][2]);
System.out.println(someNums[1][4]);
}
}
Tags: cs 106 lab
|
 |
 |
 |
 |
|
 |
 |



 |
knowledgebox
suebriquet | |
 |
 |
 |
 |
|
 |
 |
Algorithm – a solution to a problem
----------------------------------------------------------
Pseudocode –English with programming syntax used where needed
Arrays –one dimensional arrays
Get 1000 names from a user. Set them in reverse order.
name[0] = “Mark”;
name[1] = “Sue”;
name[2] = “Anne”;
.
.
Name[999]=”Liv”;
String[]name = new String[1000];
For Loop
----------------------------------------------------------
For(int i = 0; i<5; i++) {
System.out.println(name[i]);
}
int I initializes the value of the condition
i<5 is the condition
i++ how value is changed
Two dimensional array
----------------------------------------------------------
String[][]excelExample = new String[5][4];
|
 |
 |
 |
 |
|
 |
 |
|
 |
 |