CSC212 Lab 3 2014
--D. Thiebaut (talk) 16:00, 10 September 2014 (EDT)
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, which is presented in this page. Using the classes Bird and Dog as example, create a new class called Snake, 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.
- as well as several new methods:
- setLength( int ), which can be used to modify the length of the snake as it grows
- isAdult(), which returns true or false depending if the length is longer than 70 inches.