Difference between revisions of "CSC212 Lab 3 2014"
(→Arrays) |
(→Submit Snake.java (due date 9/18/14 @ 1:00 p.m.)) |
||
(23 intermediate revisions by the same user not shown) | |||
Line 57: | Line 57: | ||
</pre></code> | </pre></code> | ||
<br /> | <br /> | ||
− | Every element is the sum of the previous 2. You need to start with putting 1 and 1 in the first 2 cells, then compute | + | Every element is the sum of the previous 2. You need to start with putting 1 and 1 in the first 2 cells of the array, then compute |
<br /> | <br /> | ||
:::<source lang="java"> | :::<source lang="java"> | ||
Line 67: | Line 67: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | </ol> | ||
+ | <br /> | ||
+ | =Classes and Objects= | ||
+ | <br /> | ||
+ | ==Student== | ||
+ | <ol> | ||
+ | <br /> | ||
+ | <li /> Take the '''Student''' class from [[CSC212_Examples_for_Lecture_3 | here]] and add a new field to the class ('''''Note:''' You do not need to use the array of students in this section''). The field is called GPA, and is a '''double'''. It represents the student's GPA. Modify the constructor and ''display()'' methods, as well as the ''main()'' method to test that your new class works well. | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <li /> Add a new method that can be used to change the GPA. The method will be called ''setGPA()'' and will receive a '''double''' as a parameter, and will change the GPA of a given student. Make sure you test this method in the ''main()'' method. Here is an example of the way you would test this in ''main()'': | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | :::<source lang="java"> | ||
+ | Student s1 = new Student( "Yvonne", 23, 3.4 ); // notice the new constructor | ||
+ | s1.display(); | ||
+ | s1.setGPA( 4.0 ); | ||
+ | s1.display(); | ||
+ | </source> | ||
+ | |||
+ | </ol> | ||
+ | <br /> | ||
+ | =Inheritance= | ||
+ | <br /> | ||
+ | In this section you will create a new class derived from '''Animal'''. Animal.java is presented in [[CSC212_Examples_for_Lecture_3| this page]]. | ||
+ | |||
+ | * Using the classes '''Bird''' and '''Dog''' as examples, create a new class called '''Snake''' derived from '''Animal''', which will have several new ''fields'': | ||
+ | ** A '''length''', integer. | ||
+ | ** An '''ambient temperature''', also an integer, that needs to be maintained for it to be happy. | ||
+ | <br /> | ||
+ | * Your Snake class will also contain new several new '''methods''': | ||
+ | ** ''addLength( int )'', which can be used to modify the length of the snake as it grows. ''addLength()'' adds whatever int is passed as argument to the length of the snake. If its current length is 10, then ''addLength( 3 )'' will change the length to 13. | ||
+ | ** ''isAdult()'', which returns ''true'' or ''false'' depending if the length is longer than 70 inches. | ||
+ | <br /> | ||
+ | * Test your new Snake class by adding code to your main() method that will create a snake, and simulate the snake starting with a length of 50, and growing by 3 inches every year, over 10 years. | ||
+ | <br /> | ||
+ | :::<source lang="java"> | ||
+ | // create Snake object, named "python", 1 year old, not vaccinated or tattooed, | ||
+ | // 50 inch long, and liking a temperature of 80 degrees F. | ||
+ | Snake s1 = new Snake( "python", 1, false, false, 50, 80 ); | ||
+ | |||
+ | for ( int year=0; year < 10; year++ ) { | ||
+ | s1.addLength( 3 ); | ||
+ | s1.displayBasicInfo(); | ||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | * The output of this loop could look something like this: | ||
+ | <br /> | ||
+ | <code><pre> | ||
+ | python (1), not tattooed, not vaccinated, 53-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 56-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 59-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 62-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 65-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 68-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 71-inches long, best at 80 degrees F, adult. | ||
+ | python (1), not tattooed, not vaccinated, 74-inches long, best at 80 degrees F, adult. | ||
+ | python (1), not tattooed, not vaccinated, 77-inches long, best at 80 degrees F, adult. | ||
+ | python (1), not tattooed, not vaccinated, 80-inches long, best at 80 degrees F, adult. | ||
+ | </pre></code> | ||
+ | |||
+ | <br /> | ||
+ | <tanbox>Important note: make sure your constructor for Snake expects parameters in the same order as shown below, because this is the way Moodle will try to test your program! | ||
+ | <br /> | ||
+ | :::<source lang="java"> | ||
+ | // constructor: | ||
+ | // nm is the name, ag represents the ag, vacc, whether the animal is vaccinated, ttoo, whether | ||
+ | // it is tattooed, len, the length, and tmprtre, the best temperature for the snake. | ||
+ | Snake(String nm, int ag, boolean vacc, boolean ttoo, int len, int tmprtre ) { | ||
+ | // your code here... | ||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
+ | </tanbox> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Submit Snake.java (due date 9/18/14 @ 1:00 p.m.)= | ||
+ | <br /> | ||
+ | * Connect to Moodle, locate the CSC212 class and the '''Lab 3''' test. | ||
+ | * Your program is going to be tested by Moodle in this fashion: | ||
+ | ** Moodle already has '''Animal.java''', the original one from my page. | ||
+ | ** Moodle has been given a new class called '''SnakeTest.java''', which tests your submitted Snake.java. Its code is given below, so that you can test your program as well by running SnakeTest.java yourself. | ||
+ | ** Moodle expects that the output of SnakeTest.java will look something ''very close'' to this: | ||
+ | <br /> | ||
+ | :::<source lang="text"> | ||
+ | python (1), not tattooed, not vaccinated, 53-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 56-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 59-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 62-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 65-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 68-inches long, best at 80 degrees F, not adult. | ||
+ | python (1), not tattooed, not vaccinated, 71-inches long, best at 80 degrees F, adult. | ||
+ | python (1), not tattooed, not vaccinated, 74-inches long, best at 80 degrees F, adult. | ||
+ | python (1), not tattooed, not vaccinated, 77-inches long, best at 80 degrees F, adult. | ||
+ | python (1), not tattooed, not vaccinated, 80-inches long, best at 80 degrees F, adult. | ||
+ | </source> | ||
+ | <br /> | ||
+ | ** If it finds the right information output, your Snake.java will get 100/100, otherwise 0/100 | ||
+ | * For reference, I'm including '''SnakeTest.java''' below. Notice that it will ''inherit'' your submitted Snake.java file. | ||
+ | <br /> | ||
+ | :::<source lang="java"> | ||
+ | |||
+ | public class SnakeTest extends Snake { | ||
+ | |||
+ | SnakeTest(String n, int a, boolean v, boolean t, int l, int temp) { | ||
+ | super(n, a, v, t, l, temp); | ||
+ | } | ||
+ | |||
+ | public void displayBasicInfo() { | ||
+ | String v = "vaccinated"; | ||
+ | if (!isVaccinated) | ||
+ | v = "not " + v; | ||
+ | String t = "tattooed"; | ||
+ | if (!isTattooed) | ||
+ | t = "not " + t; | ||
+ | System.out.print(String.format("%s (%d), %s, %s", name, age, t, v)); | ||
+ | |||
+ | String adult; | ||
+ | if (isAdult()) | ||
+ | adult = "adult"; | ||
+ | else | ||
+ | adult = "not adult"; | ||
+ | System.out.println(String.format( | ||
+ | ", %d-inches long, best at %d degrees F, %s.", length, | ||
+ | temperature, adult)); | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | // create Snake object, named "python", not vaccinated or tattooed, | ||
+ | // 50 inch long, and liking a temperature of 80 degrees F. | ||
+ | SnakeTest s1 = new SnakeTest("python", 1, false, false, 50, 80); | ||
+ | |||
+ | for (int year = 0; year < 10; year++) { | ||
+ | s1.addLength(3); | ||
+ | s1.displayBasicInfo(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
+ | The output will be in the form of two numbers, reporting the number of "not adult" and "adult" strings found in the output. | ||
+ | |||
+ | 6 | ||
+ | 4 | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:Labs]][[Category:CSC212]][[Category:Java]] |
Latest revision as of 20:17, 11 September 2014
--D. Thiebaut (talk) 16:00, 10 September 2014 (EDT)
Contents
This lab presents several problems that deal with arryas, classes, objects, and inheritance.
Arrays
Takes this simple example program and modify it in the different ways suggested.
public class Lab3_1 { public static void main( String[] args ) { int table[] = new int[10]; } }
- Make your program store numbers in the table using a for-loop. The numbers should be 0, 2, 4, 6, ...18. Make your program display the contents of the array table with a for-loop.
- Make your program store random numbers in the array, display the contents of the array, and find the largest element, and its index. Using random numbers in Java requires importing a new module into the program, generating a new object of type Random. See below for a way to start:
import java.util.Random; public class Lab3_1 { public static void main( String[] args ) { int table[] = new int[10]; Random randomGenerator = new Random(); for (int i=0; i<table.length; i++ ) table[i] = randomGenerator.nextInt(100); } }
- Make your program create 2 arrays of 10 elements. It will initialize the first array with the numbers 1 to 10, and the second with 0s. It will display both. Then it will copy all the numbers from the first array to the second one, and overwrite all the 0s in the process. It will display both arrays at the end.
- Make your program create an array Fib of size 10 elements, and store the Fibonacci series in it. The Fibonacci series can be defined as follows:
1 1 2 3 5 8 13 21 ...
Every element is the sum of the previous 2. You need to start with putting 1 and 1 in the first 2 cells of the array, then compute
// in some loop Fib[i] = Fib[i-1] + Fib[i-2]
Classes and Objects
Student
- Take the Student class from here and add a new field to the class (Note: You do not need to use the array of students in this section). The field is called GPA, and is a double. It represents the student's GPA. Modify the constructor and display() methods, as well as the main() method to test that your new class works well.
- Add a new method that can be used to change the GPA. The method will be called setGPA() and will receive a double as a parameter, and will change the GPA of a given student. Make sure you test this method in the main() method. Here is an example of the way you would test this in main():
Student s1 = new Student( "Yvonne", 23, 3.4 ); // notice the new constructor s1.display(); s1.setGPA( 4.0 ); s1.display();
Inheritance
In this section you will create a new class derived from Animal. Animal.java is presented in this page.
- Using the classes Bird and Dog as examples, create a new class called Snake derived from Animal, which will have several new fields:
- A length, integer.
- An ambient temperature, also an integer, that needs to be maintained for it to be happy.
- Your Snake class will also contain new several new methods:
- addLength( int ), which can be used to modify the length of the snake as it grows. addLength() adds whatever int is passed as argument to the length of the snake. If its current length is 10, then addLength( 3 ) will change the length to 13.
- isAdult(), which returns true or false depending if the length is longer than 70 inches.
- Test your new Snake class by adding code to your main() method that will create a snake, and simulate the snake starting with a length of 50, and growing by 3 inches every year, over 10 years.
// create Snake object, named "python", 1 year old, not vaccinated or tattooed, // 50 inch long, and liking a temperature of 80 degrees F. Snake s1 = new Snake( "python", 1, false, false, 50, 80 ); for ( int year=0; year < 10; year++ ) { s1.addLength( 3 ); s1.displayBasicInfo(); }
- The output of this loop could look something like this:
python (1), not tattooed, not vaccinated, 53-inches long, best at 80 degrees F, not adult.
python (1), not tattooed, not vaccinated, 56-inches long, best at 80 degrees F, not adult.
python (1), not tattooed, not vaccinated, 59-inches long, best at 80 degrees F, not adult.
python (1), not tattooed, not vaccinated, 62-inches long, best at 80 degrees F, not adult.
python (1), not tattooed, not vaccinated, 65-inches long, best at 80 degrees F, not adult.
python (1), not tattooed, not vaccinated, 68-inches long, best at 80 degrees F, not adult.
python (1), not tattooed, not vaccinated, 71-inches long, best at 80 degrees F, adult.
python (1), not tattooed, not vaccinated, 74-inches long, best at 80 degrees F, adult.
python (1), not tattooed, not vaccinated, 77-inches long, best at 80 degrees F, adult.
python (1), not tattooed, not vaccinated, 80-inches long, best at 80 degrees F, adult.
Important note: make sure your constructor for Snake expects parameters in the same order as shown below, because this is the way Moodle will try to test your program!
// constructor: // nm is the name, ag represents the ag, vacc, whether the animal is vaccinated, ttoo, whether // it is tattooed, len, the length, and tmprtre, the best temperature for the snake. Snake(String nm, int ag, boolean vacc, boolean ttoo, int len, int tmprtre ) { // your code here... }
Submit Snake.java (due date 9/18/14 @ 1:00 p.m.)
- Connect to Moodle, locate the CSC212 class and the Lab 3 test.
- Your program is going to be tested by Moodle in this fashion:
- Moodle already has Animal.java, the original one from my page.
- Moodle has been given a new class called SnakeTest.java, which tests your submitted Snake.java. Its code is given below, so that you can test your program as well by running SnakeTest.java yourself.
- Moodle expects that the output of SnakeTest.java will look something very close to this:
python (1), not tattooed, not vaccinated, 53-inches long, best at 80 degrees F, not adult. python (1), not tattooed, not vaccinated, 56-inches long, best at 80 degrees F, not adult. python (1), not tattooed, not vaccinated, 59-inches long, best at 80 degrees F, not adult. python (1), not tattooed, not vaccinated, 62-inches long, best at 80 degrees F, not adult. python (1), not tattooed, not vaccinated, 65-inches long, best at 80 degrees F, not adult. python (1), not tattooed, not vaccinated, 68-inches long, best at 80 degrees F, not adult. python (1), not tattooed, not vaccinated, 71-inches long, best at 80 degrees F, adult. python (1), not tattooed, not vaccinated, 74-inches long, best at 80 degrees F, adult. python (1), not tattooed, not vaccinated, 77-inches long, best at 80 degrees F, adult. python (1), not tattooed, not vaccinated, 80-inches long, best at 80 degrees F, adult.
- If it finds the right information output, your Snake.java will get 100/100, otherwise 0/100
- For reference, I'm including SnakeTest.java below. Notice that it will inherit your submitted Snake.java file.
public class SnakeTest extends Snake { SnakeTest(String n, int a, boolean v, boolean t, int l, int temp) { super(n, a, v, t, l, temp); } public void displayBasicInfo() { String v = "vaccinated"; if (!isVaccinated) v = "not " + v; String t = "tattooed"; if (!isTattooed) t = "not " + t; System.out.print(String.format("%s (%d), %s, %s", name, age, t, v)); String adult; if (isAdult()) adult = "adult"; else adult = "not adult"; System.out.println(String.format( ", %d-inches long, best at %d degrees F, %s.", length, temperature, adult)); } public static void main(String[] args) { // create Snake object, named "python", not vaccinated or tattooed, // 50 inch long, and liking a temperature of 80 degrees F. SnakeTest s1 = new SnakeTest("python", 1, false, false, 50, 80); for (int year = 0; year < 10; year++) { s1.addLength(3); s1.displayBasicInfo(); } } }
The output will be in the form of two numbers, reporting the number of "not adult" and "adult" strings found in the output.
6 4