Java reads all the class definitions (variables, constructors, methods, etc.) before executing public static void main (String[] args). This means you can have the main program inside a class, and that the program will start with one copy of that particular class. This makes me wonder if you could refer to itself in an array.
The code created a dog object uses variables to define the dog and it's actions, then it 'prints' the strings. The variable x, which is defined as a interger, 0, is used to determine the number of dogs. that are created.Thursday, January 29, 2015
Friday, January 16, 2015
head first java chapter 2
This chapter helped me understand the basics of Object Oriented Programming (OOP). I really like the concept of OOP, it turns programming to an organized set of classes and objects that can be changed or easily.
Some other things I learned include:
Some other things I learned include:
- main() should only be used to test real classes and/or to launch and start a Java application
- Objects can "communtcate" to teach other by calling methods on each other
- "Global" variables don't really exist in Java OOP, but later we'll learn how usingpublic and static can make a variable behave like a global one
- Subclasses can inherit instance variables and methods from superclasses
Monday, January 12, 2015
what i learned chapter 1 java
In the first chapter of Headfirst Java, the two biggest things I learned about are casts and declaring arrays. Casts allow you to change numbers from one data type to another, such as from Float to Int.
Example:
float x = 32.25f;
if ((int) x == 32) {
System.out.println("This will print because 32.25 rounds to 32.")
}
if (x==32) {
System.out.println("This won't print because x is still 32.25.")
}
Arrays in Java are created somewhat differently than other languages- You have to tell the code you want an array after the primitive type. This means the array will only accept one type of data (String, integer, etc).
String[] animals = {"cats", "dogs", "pigs"}
int[] numbers = {1, 30, 25, 22.5} // This will throw an error, since 22.5 is a float in an integer array.
Example:
float x = 32.25f;
if ((int) x == 32) {
System.out.println("This will print because 32.25 rounds to 32.")
}
if (x==32) {
System.out.println("This won't print because x is still 32.25.")
}
Arrays in Java are created somewhat differently than other languages- You have to tell the code you want an array after the primitive type. This means the array will only accept one type of data (String, integer, etc).
String[] animals = {"cats", "dogs", "pigs"}
int[] numbers = {1, 30, 25, 22.5} // This will throw an error, since 22.5 is a float in an integer array.
Sunday, January 11, 2015
beersong java head first
Beersong
I fixed a code that prints the words to the Beer Song.
Original code:
public class BeerSong {
public static void main(String[] args) {
int beernum = 99;
String word = "bottles";
while (beernum>0) {
if (beernum==1) {
word = "bottle";
}
System.out.println(beernum + " " + word + " of beer on the wall");
System.out.println(beernum + " " + word + " of beer");
System.out.println("Take one down");
System.out.println("Pass it around");
beernum = beernum - 1;
if (beernum > 0) {
System.out.println(beernum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}
public class BeerSong {
public static void main(String[] args) {
int beernum = 99;
String word = "bottles";
while (beernum>0) {
System.out.println(beernum + " " + word + " of beer on the wall");
System.out.println(beernum + " " + word + " of beer");
System.out.println("Take one down");
System.out.println("Pass it around");
beernum = beernum - 1;
if (beernum==1) {
word = "bottle";
}
if (beernum!=0) {
System.out.println(beernum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}
Original code:
public class BeerSong {
public static void main(String[] args) {
int beernum = 99;
String word = "bottles";
while (beernum>0) {
if (beernum==1) {
word = "bottle";
}
System.out.println(beernum + " " + word + " of beer on the wall");
System.out.println(beernum + " " + word + " of beer");
System.out.println("Take one down");
System.out.println("Pass it around");
beernum = beernum - 1;
if (beernum > 0) {
System.out.println(beernum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}
This works well, towards the end it prints "Pass it around, 1 bottles of beer on the wall" because the variable "word" is set at the beginning of the loop rather than after the beer count changes so it doesn't change the word bottle.
public static void main(String[] args) {
int beernum = 99;
String word = "bottles";
while (beernum>0) {
System.out.println(beernum + " " + word + " of beer on the wall");
System.out.println(beernum + " " + word + " of beer");
System.out.println("Take one down");
System.out.println("Pass it around");
beernum = beernum - 1;
if (beernum==1) {
word = "bottle";
}
if (beernum!=0) {
System.out.println(beernum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
}
}
}
}
Subscribe to:
Comments (Atom)