Last active
January 11, 2018 23:14
-
-
Save camtheman256/bee36e8aa9a770f7708aed1f6c6adcf0 to your computer and use it in GitHub Desktop.
Revisions
-
camtheman256 revised this gist
Jan 11, 2018 . No changes.There are no files selected for viewing
-
camtheman256 renamed this gist
Jan 11, 2018 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,8 +2,6 @@ By Cameron Kleiman, Lauren Mangibin, Nirali Devgan ## Objectives: 1. Learn how to create a class to represent an object -
camtheman256 revised this gist
Jan 11, 2018 . 2 changed files with 57 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ public class Pencil { private int pencilSharp; private String pencilColor; public Pencil(){ pencilSharp = 3; pencilColor = "yellow"; } public Pencil(int sharp, String color) { pencilSharp = sharp; pencilColor = color; } public int sharpen () { pencilSharp = 3; return pencilSharp; } public String paint (String newColor) { pencilColor = newColor; return pencilColor; } public int getSharpness() { return pencilSharp; } public String getColor() { return pencilColor; } public String write() { if(pencilSharp > 0) { pencilSharp--; return "Success. You have written!"; } else { return "Your pencil is too dull to write. Sharpen it!"; } } public String toString(){ return ("You have a sharpness of " + pencilSharp + ". It is " + pencilColor + "."); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ class PencilRunner { public static void main(String args[]) { Pencil regular = new Pencil(); System.out.println(regular); regular.paint("red"); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.getSharpness()); System.out.println(regular.sharpen()); } } -
camtheman256 revised this gist
Sep 21, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,8 @@ By Cameron Kleiman, Lauren Mangibin, Nirali Devgan The link to the final code is at https://gist.github.com/camtheman256/1586cba94bd6c98d46091c098b82568e; ## Objectives: 1. Learn how to create a class to represent an object @@ -174,6 +176,4 @@ Tip: Try and call `write()` one more time on `regular`. It should give you an er Have fun `paint()`-ing, `write()`-ing, and `sharpen()`-ing your `Pencil`. Create multiple pencils and enjoy. Make sure to wrap your method calls in `System.out.println()` to confirm everthing outputs corrrectly. Use the `getSharpness()` and `getColor()` methods to read out the properties of your pencil. -
camtheman256 revised this gist
Sep 21, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -176,4 +176,4 @@ Have fun `paint()`-ing, `write()`-ing, and `sharpen()`-ing your `Pencil`. Create Make sure to wrap your method calls in `System.out.println()` to confirm everthing outputs corrrectly. Use the `getSharpness()` and `getColor()` methods to read out the properties of your pencil. The link to the final code is at https://gist.github.com/camtheman256/1586cba94bd6c98d46091c098b82568e; -
camtheman256 revised this gist
Sep 21, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -176,4 +176,4 @@ Have fun `paint()`-ing, `write()`-ing, and `sharpen()`-ing your `Pencil`. Create Make sure to wrap your method calls in `System.out.println()` to confirm everthing outputs corrrectly. Use the `getSharpness()` and `getColor()` methods to read out the properties of your pencil. The link to the final code is at [https://gist.github.com/camtheman256/1586cba94bd6c98d46091c098b82568e]; -
camtheman256 revised this gist
Sep 21, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -176,4 +176,4 @@ Have fun `paint()`-ing, `write()`-ing, and `sharpen()`-ing your `Pencil`. Create Make sure to wrap your method calls in `System.out.println()` to confirm everthing outputs corrrectly. Use the `getSharpness()` and `getColor()` methods to read out the properties of your pencil. The link to the final code is at (https://gist.github.com/camtheman256/1586cba94bd6c98d46091c098b82568e); -
camtheman256 revised this gist
Sep 21, 2016 . 3 changed files with 1 addition and 57 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,44 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -176,3 +176,4 @@ Have fun `paint()`-ing, `write()`-ing, and `sharpen()`-ing your `Pencil`. Create Make sure to wrap your method calls in `System.out.println()` to confirm everthing outputs corrrectly. Use the `getSharpness()` and `getColor()` methods to read out the properties of your pencil. The link to the final code is at (); -
camtheman256 revised this gist
Sep 21, 2016 . No changes.There are no files selected for viewing
-
camtheman256 revised this gist
Sep 21, 2016 . No changes.There are no files selected for viewing
-
camtheman256 created this gist
Sep 21, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ public class Pencil { private int pencilSharp; private String pencilColor; public Pencil(){ pencilSharp = 3; pencilColor = "yellow"; } public Pencil(int sharp, String color) { pencilSharp = sharp; pencilColor = color; } public int sharpen () { pencilSharp = 3; return pencilSharp; } public String paint (String newColor) { pencilColor = newColor; return pencilColor; } public int getSharpness() { return pencilSharp; } public String getColor() { return pencilColor; } public String write() { if(pencilSharp > 0) { pencilSharp--; return "Success. You have written!"; } else { return "Your pencil is too dull to write. Sharpen it!"; } } public String toString(){ return ("You have a sharpness of " + pencilSharp + ". It is " + pencilColor + "."); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ class PencilRunner { public static void main(String args[]) { Pencil regular = new Pencil(); System.out.println(regular); regular.paint("red"); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.getSharpness()); System.out.println(regular.sharpen()); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,178 @@ # Pencil Object Tutorial By Cameron Kleiman, Lauren Mangibin, Nirali Devgan ## Objectives: 1. Learn how to create a class to represent an object 2. Add private instance variables in your class 3. Add constructors to your class 4. Add accessor and mutator methods for your class 5. Add a `toString()` method to your class ## Lab: For our tutorial, we will be creating a Java object class based on a pencil. Our pencil will have a few properties such as sharpness and color. The maximum sharpness is 3, and the default color is yellow. ### Let's Get Started First, let's set up our Pencil object and give it variables of sharpness (an `int`) and color (a `String`). These are our object class's properties ```java public class Pencil { private int pencilSharp; private String pencilColor; } ``` ### Setting up our `Pencil` Next, we need to give our `Pencil` class a default constructor, or assign it default values, should the user choose to specify none. ```java public class Pencil { // ... public Pencil(){ pencilSharp = 3; pencilColor = "yellow"; } } ``` Afterwards, create an initialization constructor in order to create your own unique pencil. ```java public class Pencil { // ... public Pencil(){ pencilSharp = 3; pencilColor = "yellow"; } public Pencil(int sharp, String color) { pencilSharp = sharp; pencilColor = color; } } ``` Don't worry about the fact that we have two `Pencil` constructors. That's just the way Java works. You need to specify a default constructor and one that has options. ### Adding a mutator method Add a mutator method to change the color and the sharpness. A mutator method changes internal values within our object. While this may seem redundant, it is necessary so the user can have direct access to the object's properties. We will only allow the user to do realistic operations on their `Pencil`. For example, the user can sharpen their `Pencil` to give it a sharpness value of 3, or they can paint their pencil to change its colors. These are both mutator methods. ```java public class Pencil { //... public int sharpen() { pencilSharp = 3; return pencilSharp; } public String paint(String newColor) { pencilColor = newColor; return pencilColor; } } ``` ### Accessor Methods What if the user wants to know the sharpness or color of their `Pencil`? These are where accessor methods come in, as they allow the user to access certain properties of `Pencil`. Add `int` and `String` to the method names to tell Java what type of value will be returned. ```java public class Pencil { //... public int getSharpness() { return pencilSharp; } public String getColor() { return pencilColor; } } ``` ### Adding a method Now, we want the user to be able to do something with their `Pencil`. They should be able to `write()` with their `Pencil`. However, they will not be able to write with their `Pencil` if it is dull, so we will want to remind them to `sharpen()` their `Pencil`. This will return a `String`, so we will need to tell Java that. ```java public class Pencil { //... public String write() { if(pencilSharp > 0) { pencilSharp--; return "Success. You have written!"; } else { return "Your pencil is too dull to write. Sharpen it!"; } } } ``` However, we will want to have something to tell Java if someone tries to call `System.out.println()` (or any other method requiring a general summary of our `Pencil`) on our `Pencil` object. Next, we will display the color and sharpness of the pencil using a `toString()` method. ```java public class Pencil { //... public String toString(){ return ("You have a sharpness of " + pencilSharp + ". It is " + pencilColor + "."); } } ``` ## Putting it all together Now, create another `PencilRunner.java` program in the same folder to utilize some of our `Pencil`'s features. Create a new `Pencil` with a variable of your choosing using the `new` keyword. Initialize it using the default constructor. Print out its default values using the `toString()` method. You don't need to put `regular.toString()` in the `System.out.println()` statement because Java knows to automatically execute the `toString()` function when you try to access the `Pencil` object as a variable. ```java class PencilRunner { public static void main(String args[]) { Pencil regular = new Pencil(); System.out.println(regular); } } ``` Finally, let's change our `Pencil` color and write three times with `regular`. We need to wrap our `write()` functions in a `System.out.println()` to make sure they work properly. ```java class PencilRunner { public static void main(String args[]) { Pencil regular = new Pencil(); System.out.println(regular); regular.paint("red"); System.out.println(regular.write()); System.out.println(regular.write()); System.out.println(regular.write()); } } ``` Tip: Try and call `write()` one more time on `regular`. It should give you an error statement in the console. Try using `sharpen()` to reset your sharpness. Have fun `paint()`-ing, `write()`-ing, and `sharpen()`-ing your `Pencil`. Create multiple pencils and enjoy. Make sure to wrap your method calls in `System.out.println()` to confirm everthing outputs corrrectly. Use the `getSharpness()` and `getColor()` methods to read out the properties of your pencil.