<li>Create a private variable of int type called year</li>
<li>Create a private variable of String type called make</li>
</ul>
</li>
<li>Create accessor methods for all data members.</li>
<li>Create mutator methods for all data methods.</li>
</ol>
<pre><codeclass="language-java">public class car { // begin car
private int year;
private String make;
public int getYear(void) {
return year;
}
public String getMake() {
return make;
}
public void setYear(int y) {
if (y > 1890) {
year = y;
} else {
System.out.println(y + " is not a valid year.");
}
}
public void setMake(String m) {
make = m;
}
}</code></pre>
<p>Local variables are only existent within the curly braces that it is defined in.</p>
<h2>If Statements and Boolean Expressions</h2>
<p>Boolean expressions return a boolean</p>
<pre><codeclass="language-java">1 < 4; // 1 is less than 4: TRUE
3 > 5; // 3 is greater than 5: FALSE
5 == 5; // 5 is equal to 5: TRUE
5 != 5; // 5 is not equal to 5: FALSE
1 >= 1; // 1 is greater than or equal to 1: TRUE
5 <= 1; // 5 is less than or equal to 1: FALSE</code></pre>
<p>If statements only occur if the boolean expression is true, otherwise the <code>else</code> block is executed.</p>
<pre><codeclass="language-java">if (true) {
System.out.println("I am always printed");
} else {
System.out.println("I am never printed");
}</code></pre>
<p>You can only have one <code>else</code> per <code>if</code>. If you have an <code>if</code> you don't necessarily need an <code>else</code></p>
<h2>Local vs Class Variables</h2>
<p>If you have a local variable and the class variable sharing the same name, then the local variable is always used first.</p>
<pre><codeclass="language-java">public class car { // begin car
private int year;
public void setYear(int year) {
year = year;
}
}</code></pre>
<p>This is a redundant statement, it makes the argument that is passed in equal to itself.</p>
<p>To avoid this situation, use the keyword <code>this</code> to access the class variable</p>
<pre><codeclass="language-java">public class car {
private int year;
public void setYear(int year) {
this.year = year;
}
}</code></pre>
<p>The code above runs as expected.</p>
<p>Rewriting our class with <code>this</code></p>
<pre><codeclass="language-java">public class car { // begin car
private int year;
private String make;
public int getYear(void) {
return year;
}
public String getMake() {
return make;
}
public void setYear(int year) {
if (y > 1890) {
this.year = year;
} else {
System.out.println(y + " is not a valid year.");
}
}
public void setMake(String make) {
this.make = make;
}
}</code></pre>
<h2>Unreachable Code</h2>
<p>When the code hits a <code>return</code> statement, it stops executing the rest of the code in the method. Also throws an Unreachable Code Error.</p>
<pre><codeclass="language-java">public int add(int x, int y) {
return x + y;
System.out.println("x + y = " + x + y);
}
add();
System.out.println("Hello");</code></pre>
<p>Here the code above will not compile, though assuming the error doesn't exist then it would only print out "Hello"</p>
<h2>Constructors</h2>
<p>You cannot have a private or protected constructors.</p>
<p>Constructors are used to initialize your objects.</p>
<p>You want to have the class variables to the left of the assignment statement.</p>
<pre><codeclass="language-java">public class car {
private int year;
private String make;
car() {
year = 1890;
make = "Ford";
}
car(int year, String make) {
this.year = year;
this.make = make;
}
}</code></pre>
<h2>Testers</h2>
<p>Testers are useful to check that the class is implemented correctly. Both the tester and the class have to be in the same folder/directory.</p>
<pre><codeclass="language-java">public class carTester {
public static void main(String[] args) {
Car myCar; // Declaration
myCar = new Car(); // Initilization
Car yourCar = new Car(2009, "Hyundai"); // Declaration + Initialization
}
}</code></pre>
<h2>More about classes</h2>
<pre><codeclass="language-java">public class Car {
private String name;
private int odometer;
public void setOdometer(int od) {
odometer = od;
}
public void setName(String n) {
this.name = n;
}
public void changeOilRequest(String name, int od) {
if (name == this.name) {
int difference = od - this.odometer;
if (difference > = 3000) {
// You can call other methods in the class
setOdo(od); // Equivalent to "this.setOdo(od);"
this.odometer = od;
System.out.println("Ready for oil change.");
} else {
System.out.println(name + " not ready for oil change.")
}
} // end if
} // end changeOil request
} // end class</code></pre>
<p>To call public methods outside the class use the variable name to do so.</p>
<pre><codeclass="language-java">public class CarTester {
public static void main(String[] args) {
Car myCar = new Car();
myCar.setName("Honda")
myCar.changeOilRequest("Honda", 3400);
}
}</code></pre>
<h2>Math library</h2>
<p>The <code>ceil</code> method rounds up while the <code>floor</code> method runs down.</p>