Sunday, October 28, 2012

Debug letters and symbols

   I spent over an hour and a half typing in a long program in notepad then spent another half hour debugging . It mainly consisted of missing letters and symbols or the capitalization of letters. This is where a program like Eclipse would have helped but I have been trying to just use notepad for the examples in the book I am going through. It's been a long day but I am glad I managed to get in a couple hours studying.

10,000 hour to mastery.
10,000 - 315 = 9685 hours left

Thursday, October 25, 2012

Button Layout

  I have been practicing a bit with the layout manager  which can be very annoying. So I decided to try hard coding the locations and sizes of the button. It's a lot easier than figuring out what the layout manager is going to do. Perhaps when I am more familiar with settings I can use the manager.
The Code:
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Button1 {
//get it all started
public static void main(String[] args) {
Button1 gui = new Button1();
gui.go();
}

public void go() {
JFrame frame = new JFrame();//need a frame to put it all in
JPanel panelM = new JPanel();//needed to put things on the frame

panelM.setLayout(null);// not going to use the layout manager

//create some buttons to put in the Panel
JButton buttonT = new JButton("This is top");
JButton buttonC = new JButton("This is center");
JButton buttonB = new JButton("This is bottom");

//set place and how big for all buttons
buttonT.setLocation(79, 1);
buttonT.setSize(150, 50);

buttonC.setLocation(79, 166);
buttonC.setSize(150, 50);

buttonB.setLocation(79, 317);
buttonB.setSize(150, 50);

//now we put all the buttons on the panel
panelM.add(buttonT);
panelM.add(buttonC);
panelM.add(buttonB);

//gray looks dull so lets add color
panelM.setBackground(new Color(255,255,100));

//now set everything for frame
frame.setSize(308, 400);
frame.getContentPane().add(BorderLayout.CENTER, panelM);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// end of go
}//end of class Button1

 10,000 hour to mastery.
10,000 - 311 = 9689 hours left

Tuesday, October 23, 2012

Assignments completed so far

 
  A few postings back I had a list of assignments that a beginner might want to try to accomplish . I thought that I should try to complete them myself. So far I have done from 1 to 11 out of 16 using NotePad and command prompt javac compiling. It's a lot harder when you don't have something like Eclipse helping you but I felt it was important for a learning experience.

 Assignment 11 :Create a card game that deals one card out to each player (2,3,4,5,6,7,8,9,10,Jack,Queen,King,Ace).

Here is the code:
// A card game in which two players get one card each. High card wins.
// Three possible outcomes. player1 wins, player2 wins or a tie.

public class OneCard{
static int player1;
static int player2;

public static void main(String[] args){

// loop it 9 times to test
for(int i = 0; i < 9; i++){
player1 = (int) ((Math.random()*13)+2);//2 to 14, this may not be correct
player2 = (int) ((Math.random()*13)+2);

String player1A = convertCard(player1);
String player2B = convertCard(player2);

System.out.println("Player one's card  is "+ player1A + " -- Player two's card is " + player2B);

if ( player1 > player2){
System.out.println("Player one is the winner!");
}else if (player1 < player2){
System.out.println("Player two is the winner!");
}else{
System.out.println("We have a tie, both of you are losers");
}// end of ifs
}// end of for loop
}// end of main

  public static String convertCard(int player){
// I could have also used switch case here
int playerCard = player;
if (playerCard = = 14){
return "Ace";
}else if (playerCard = = 11){
return "Jack";
}else if  (playerCard = = 12){
return "Queen";
}else if (playerCard = = 13){
return "King";
}else{
return Integer.toString( playerCard);
}// end of ifs
}// end of convertCard method
}// end of class OneCard

Saturday, October 20, 2012

JFrame and JPanel sizes


    Adjustments needed when creating a JPanel and adding it to a JFrame. If you need an area of exactly 400 x 400 pixels for the JPanel. As you can see in the picture  the JPanel is smaller than needed. The 400 is for the whole window frame, which uses 4 pixels on left, right and bottom for the window edge. The top title bar needs 28. If you are doing graphics  such as a game and your calculations are based on 400 x 400 window in the picture, then your game is not going to be drawn correct. How Window themes would affect the frame I am not sure. Would thicker edges make for smaller JPanel area or does the thickness expand outward?

 10,000 hour to mastery.
10,000 - 306 = 9694 hours left

Friday, October 19, 2012

Main() explained

   In everything I have studied or watched ,  main method is never fully explained. I will try to explain it as I think it is.

public static void main(String[] args){
     //do stuff here
}

public : accessible.
static : only class associated , not instanced for each object.
void : does not accept a return.
main : name of method where the program is to start.
(String[] args) : accepts string and places it in an array named args. The use for this is if you want to pass arguments on start up of the program.

Example:
c:\java DogProgram Fido  Bobo // starting Dog program and passing the name Fido and Bobo


public static void main(String[] args){
     sting dog1 = args[0]; // dog1 is named Fido
     sting dog2 = args[1]; // dog2 is named Bobo
}

This is a simplified main method. Of course you should also have a check to see if any strings were passed.

Passed the 300 hour mark!

 10,000 hour to mastery.
10,000 - 302 = 9698 hours left

Wednesday, October 17, 2012

Should videos be counted towards Mastery?

    Since I started my Ten thousand hours to Mastery goal I have counted any video I watch about Java, Android and programming as time towards my goal of 10,000 hours. That may change now since I seem to be watching a lot of video that repeats subjects I have seen before in other videos. True it may reinforce the knowledge but at some point it is a waste of time that  I could spend  learning something new. So I will only count a video if I feel I have learned something. I will not stop watching them because every once in awhile I run across a well done clip.

10,000 hour to mastery.
10,000 - 298.25 = 9701.75 hours left

Wednesday, October 10, 2012

Assignments

   One of the problems with  learning to program is that there few clear assignments for challenging  and  progressing what you have learned.  I created a list of assignments that a beginner might attempt while learning most programming languages. It covers basic input / output , a little bit of arrays and logic.
I think that lists covers a lot of the knowledge you will need before moving on to the harder stuff like graphics.


Beginning assignments for learning to program.

1. Output something ("Hello World" is tradition).

2. Get input and display it with something added ("Nice to meet you" + input).

3. Get two numbers, add them, then output total.

4. Get multiple numbers and output total.

5. Include subtraction, multiplication and division functions.

6. Format numbers displayed.(example: $10,000.12).

7. Create random numbers in a given range.

8. Create number guessing game based on random numbers(One guess only).

9. Add continue guessing till guess is correct, then display number of guess.

10. Add hints such as higher and lower.

11. Create a card game that deals one card out to each player (2,3,4,5,6,7,8,9,10, Jack, Queen, King, Ace).

12. Have the card game deal out full deck while keeping track of who had the high card each deal (Card game "War");

13. Create a tic tac toe game and use text output for displaying positions.

14. Create a text game in which the player has to guess positions of objects ( example: Battleship game).

15. Create a program that takes input text then saves as a file to the hard drive.

16. Add the ability to open saved text file from hard drive. 



Monday, October 8, 2012

Getting back in to the swing

   For the past week or so I haven't put in the time I need / want to in studying Java and programming in general. I have been reluctant to do anything because I have been fixated on a possible new job. I need to rededicate myself to my  goal and remind myself why I am pursuing it.

   A question I am asking myself is this- Should I count the time spent creating images  to use in my java projects as time against my 10,000 hour  to Mastery goal? It's not really programming though I do have to think about design. I have decided that I will not count time spent creating resources . I will only count actual Java and Android study whether it's from a book , Online reading, video or programming directly.

   I have another game idea I want to create which should give me a better understanding of collision detection , placing random objects  and  keeping track of all the objects of the game. It's not much more then a game of moving a character around collecting things while avoiding others things on a two dimensional grid.  


10,000 hour to mastery.
10,000 - 289 = 9711 hours left