- Well this time it actually changes wiht what you type and it is faster.
- Php
- Javascript
- CSS
- It allowed me to start to learn php, and answer the questions.
Randy'sAPComputerScienceBlog
Tuesday, May 5, 2015
2.2.2.A Conclusion
Monday, April 13, 2015
Arrays
Arrays hold a predefined amount of elements of a predefined type. They're length cannot be changed. You can however change the values of the individual elements. To access the individual elements you have to use indexes. An arrays indexes start at 0 and go to 1 less than the length. So an int array that has a length of 5 has indexes from 0-4. It may be helpfull to iterate through all the elements in an array. You can do this with for loops and the array length.
To define an array you type: x[] y = new x[z]; where x is the type, y is the name, and z is the length.
To find the length you type: y.length; where y is the name.
To access the elements you type: y[z]; where y is the name and z is the index.
To iterate through an array type: for (int i = 0; i < y.length; i++) {} where y is the array.
To define an array you type: x[] y = new x[z]; where x is the type, y is the name, and z is the length.
To find the length you type: y.length; where y is the name.
To access the elements you type: y[z]; where y is the name and z is the index.
To iterate through an array type: for (int i = 0; i < y.length; i++) {} where y is the array.
Tuesday, March 10, 2015
Head First Java Chapter 6
In this chapter we learned more about the java api in order to fix our old battle ship game. We used the api to get arraylist which i have already been using and know about. They are a lot better than arrays because I find them easier to use and more versatile (They are slower though). I also learned about import packages from the java api. You can browse the internet to see all the classes in it. You have to know the full location or name of the file. You can also import as static which was not discussed but allows makes it so you don't have to type the name of the class to run a method or access a variable. Boolean operators were also explained like and (&&) and or (||), the equals(==) and not equals(!=), and the non short circuit and(&) and or(|).
This is the code for the battle ship game created in this chapter:
This is the code for the battle ship game created in this chapter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.util.ArrayList; public class DotCom { private ArrayList<String> locationCells; private String name; public void setLocationCells(ArrayList<String> loc) { locationCells = loc; } public void setName(String n) { //setter name = n; } public String checkYourself(String userInput) { String result = "miss"; int index = locationCells.indexOf(userInput); if (index >= 0) { locationCells.remove(index); if (locationCells.isEmpty()) { result = "kill"; System.out.println("Ouch! You sunk " + name + " : ( "); } else { result = "hit"; } // close if } // close if return result; } // close method } // close class |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import java.util.ArrayList; public class DotComBust { private GameHelper helper = new GameHelper(); private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>(); private int numOfGuesses = 0; private void setUpGame() { // first make some dot coms and give them locations DotCom one = new DotCom(); one.setName("Pets.com"); DotCom two = new DotCom(); two.setName("eToys.com"); DotCom three = new DotCom(); three.setName("Go2.com"); dotComsList.add(one); dotComsList.add(two); dotComsList.add(three); System.out.println("Your goal is to sink three dot coms."); System.out.println("Pets.com, eToys.com, Go2.com"); System.out.println("Try to sink them all in the fewest number of guesses"); for (DotCom dotComToSet : dotComsList) { ArrayList<String> newLocation = helper.placeDotCom(3); dotComToSet.setLocationCells(newLocation); } // close for loop } // close setUpGame method private void startPlaying() { while(!dotComsList.isEmpty()) { String userGuess = helper.getUserInput("Enter a guess"); checkUserGuess(userGuess); } // close while finishGame(); } // close startPlaying method private void checkUserGuess(String userGuess) { numOfGuesses++; String result = "miss"; for (DotCom dotComToTest : dotComsList) { result = dotComToTest.checkYourself(userGuess); if (result.equals("hit")) { break; } if (result.equals("kill")) { dotComsList.remove(dotComToTest); break; } } // close for System.out.println(result); } // close method private void finishGame() { System.out.println("All Dot Coms are dead! Your stock is now worthless."); if (numOfGuesses <= 18) { System.out.println("It only took you " + numOfGuesses + " guesses."); System.out.println(" You got out before your options sank."); } else { System.out.println("Took you long enough. "+ numOfGuesses + " guesses."); System.out.println("Fish are dancing with your options."); } } // close method public static void main(String[] args) { DotComBust game = new DotComBust(); game.setUpGame(); game.startPlaying(); } // close method } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | import java.io.*; import java.util.*; public class GameHelper { private static final String alphabet = "abcdefg"; private int gridLength = 7; private int gridSize = 49; private int[] grid = new int[gridSize]; private int comCount = 0; public String getUserInput(String prompt) { String inputLine = null; System.out.print(prompt + " "); try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); inputLine = is.readLine(); if (inputLine.length() == 0) return null; } catch (IOException e) { System.out.println("IOException: " + e); } return inputLine.toLowerCase(); } public ArrayList<String> placeDotCom(int comSize) { ArrayList<String> alphaCells = new ArrayList<String>(); String[] alphacoords = new String[comSize]; // holds ‘f6’ type coords String temp = null; // temporary String for concat int[] coords = new int[comSize]; // current candidate coords int attempts = 0; // current attempts counter boolean success = false; // flag = found a good location ? int location = 0; // current starting location comCount++; // nth dot com to place int incr = 1; // set horizontal increment if ((comCount % 2) == 1) { // if odd dot com (place vertically) incr = gridLength; // set vertical increment } while (!success & attempts++ < 200) { // main search loop (32) location = (int) (Math.random() * gridSize); // get random starting point //System.out.print(“ try “ + location); int x = 0; // nth position in dotcom to place success = true; // assume success while (success && x < comSize) { // look for adjacent unused spots if (grid[location] == 0) { // if not already used coords[x++] = location; // save location location += incr; // try ‘next’ adjacent if (location >= gridSize) { // out of bounds - ‘bottom’ success = false; // failure } if (x > 0 && (location % gridLength == 0)) { // out of bounds - right edge success = false; // failure } } else { // found already used location // System.out.print(“ used “ + location); success = false; // failure } } } // end while int x = 0; // turn location into alpha coords int row = 0; int column = 0; // System.out.println(“\n”); while (x < comSize) { grid[coords[x]] = 1; // mark master grid pts. as ‘used’ row = (int) (coords[x] / gridLength); // get row value column = coords[x] % gridLength; // get numeric column value temp = String.valueOf(alphabet.charAt(column)); // convert to alpha alphaCells.add(temp.concat(Integer.toString(row))); x++; // System.out.print(“ coord “+x+” = “ + alphaCells.get(x-1)); } // System.out.println(“\n”); return alphaCells; } } |
Monday, March 9, 2015
Age Verifier
I created a program to check if someone is over 18. I only used the main method just because this is a simple program and it went faster.
The first 6 lines import the necessary files for creating an OptionPane and getting the date.
From line 9 to 34 i am initializing variables for the current date and the entered date. Specifically, lines 27 to 29 are used to create the panes that get the users input.
The rest of the code from line 35 on is checking the input with the current time to decide whether the person is 18. Depending on whether they are or aren't it will print in the console and in a pane if they are 18 or not.
Currently there is nothing to handle if the person inputs a string, or the wrong format or if they click cancel. I am fairly new to JOptionPanes and don't know how to handle the cancel button. With the wrong input I could have handled it by using some if statements on the input.
The first 6 lines import the necessary files for creating an OptionPane and getting the date.
From line 9 to 34 i am initializing variables for the current date and the entered date. Specifically, lines 27 to 29 are used to create the panes that get the users input.
The rest of the code from line 35 on is checking the input with the current time to decide whether the person is 18. Depending on whether they are or aren't it will print in the console and in a pane if they are 18 or not.
Currently there is nothing to handle if the person inputs a string, or the wrong format or if they click cancel. I am fairly new to JOptionPanes and don't know how to handle the cancel button. With the wrong input I could have handled it by using some if statements on the input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String month, day, year; int imonth, iday, iyear, itmonth, itday, ityear; Date date = new Date(); DateFormat monthf = new SimpleDateFormat("MM"); DateFormat dayf = new SimpleDateFormat("dd"); DateFormat yearf = new SimpleDateFormat("yyyy"); String todaym = monthf.format(date); String todayd = dayf.format(date); String todayy = yearf.format(date); itmonth = Integer.parseInt(todaym); itday = Integer.parseInt(todayd); ityear = Integer.parseInt(todayy); month = JOptionPane.showInputDialog("Month of birth(MM)"); day = JOptionPane.showInputDialog("Day of birth(DD)"); year = JOptionPane.showInputDialog("Year of birth(YYYY)"); imonth = Integer.parseInt(month); iday = Integer.parseInt(day); iyear = Integer.parseInt(year); if (iyear - ityear == -18) { if (imonth - itmonth == 0) { if (iday - itday <= 0) { System.out.println("you are 18"); JOptionPane.showMessageDialog(null, "you are 18"); } else if (iday - itday > 0) { System.out.println("you are not 18"); JOptionPane.showMessageDialog(null, "you are not 18"); } } else if (imonth - itmonth < 0) { System.out.println("you are 18"); JOptionPane.showMessageDialog(null, "you are 18"); } else if (imonth - itmonth > 0) { System.out.println("you are not 18"); JOptionPane.showMessageDialog(null, "you are not 18"); } } else if (iyear - ityear > -18) { System.out.println("you are not 18"); JOptionPane.showMessageDialog(null, "you are not 18"); } else if (iyear - ityear < -18) { System.out.println("you are 18"); JOptionPane.showMessageDialog(null, "you are 18"); } } } |
Wednesday, March 4, 2015
Head First Java Chapter 5
In this chapter I learned about casting primitive variables, and using loops and I created a simple 1D game of battleship.
Casting Primitives:
You can change one type of variable to another by casting it or using the parse command. You cast a variable by typing (TypeOfVariable). Sometimes this isn't possible like when going from a string to an integer so you can use Integer.parseInt().
Using Loops:
When trying to do something over and over again you can use loops. There are multiple ways to use them. One is by doing this:
for (String name : nameArray) {} This code is sets name to an element in the array each time
or
for (int i = 0; i < 20; i++) {} This code sets i to a number from 0 to 19 and then iterates that amount of times.
Battleship:
This is the code that was used in this chapter to create a program like battleship that only uses a 1D array. I added some comments.
GameHelper gets the users input:
SimpleDotCom contains the list of ships and the code for if you hit or not:
Casting Primitives:
You can change one type of variable to another by casting it or using the parse command. You cast a variable by typing (TypeOfVariable). Sometimes this isn't possible like when going from a string to an integer so you can use Integer.parseInt().
Using Loops:
When trying to do something over and over again you can use loops. There are multiple ways to use them. One is by doing this:
for (String name : nameArray) {} This code is sets name to an element in the array each time
or
for (int i = 0; i < 20; i++) {} This code sets i to a number from 0 to 19 and then iterates that amount of times.
Battleship:
This is the code that was used in this chapter to create a program like battleship that only uses a 1D array. I added some comments.
GameHelper gets the users input:
SimpleDotCom contains the list of ships and the code for if you hit or not:
SimpleDotComTestDrive is the class with the main method, it utilizes the other two in order to run the whole thing:
Monday, February 9, 2015
Head First Java Chapter 4
In this chapter I learned more about methods in a class. I learned about using private to hide variables so they don't get values I don't want them to have. If they are private you can still change and view them using getters and setters. I learned that all methods need a return type whether it be void or an actual type. I learned methods can have parameters so you can pass values into it.
Private:

Getters and Setters:
Return Type & Parameters:

Dog Code:
This is the code from chapter 4. I added comments explaining what everything does and had to move the test drive class into a new class file to run it in eclipse. This code uses getters and setters, private varibles, and return types. It makes 2 dogs and makes them bark. The type of bark depends on their size.

Private:
Getters and Setters:
Return Type & Parameters:
Dog Code:
This is the code from chapter 4. I added comments explaining what everything does and had to move the test drive class into a new class file to run it in eclipse. This code uses getters and setters, private varibles, and return types. It makes 2 dogs and makes them bark. The type of bark depends on their size.
Thursday, February 5, 2015
Java Palindrome checker
I created a program the accepts a string and checks if it is a palindrome. It is not case sensitive but it will not work with sentences Unless you do not include the spaces between words. There are two classes the main chapter 3 class and the checker class. The checker class checks if it is a palindrome and if it is it prints a line saying that and if not it doesn't. the chapter 3 class get input and sends it to the checker class.
Chapter 3 Class:
Checker Class:
Chapter 3 Class:
Checker Class:
Subscribe to:
Comments (Atom)
