Java Programming

 

     

 

Monday, May 11th

 

Section 7 Lesson 2: Parameters and Overloading Methods Vocabulary:

- A type of access modifier. Permits access from anywhere.
- Used to assign initial values to instance variables of a class.
- A way to call a method with a variable number of arguments.
- Having more than one constructor or method with the same name but different arguments.
- A type of access modifier. Permits access only from inside the same class.
- A constructor that does not have any parameters.
- Used to specify accessibility for variables, methods, and classes

Submit your vocabulary to Canvas.

Please follow these modified instructions for the 7.2 practice.
You may want to refer to the Vehicle  (Links to an external site.) and VehicleDriver  (Links to an external site.) classes as examples.

Try It/Solve It:
1. Create an object class called Fish. If you are doing this in Codiva you will need to remove the main method that Codiva automatically inserts into all files. Create the following class variables: 
String typeOfFish
String name
int friendliness
Do not set values to these variables yet. These are instance variables and will be set inside the class constructors.

2. Create a public constructor (a method with the same name as the class) inside the class Fish. This constructor should take in no arguments. Inside the constructor, set typeOfFish to “Unknown” and friendliness to 3, which we are assuming is the generic friendliness of fish.

3. Create another public constructor inside the class Fish. Have this constructor use the following parameters, String n, String t,  int f.
Have the constructor assign the following values:
name equal n
typeOfFish equal t
friendliness equal f

4. Explain why it is possible to have more than one constructor with the same name and different arguments.

5. Create the following getter methods in the Fish class
getName() - takes in no arguments and returns the name of the fish.
gettypeOfFish() - takes in no arguments and returns the type of  fish.
getFriendliness() -   takes in no arguments and returns the friendliness level of the fish.

6. Create a FishDriver class that initializes 2 new fish as defined below:
a. Fish1:  Name – Amber, Type – AngelFish, Friendliness level – 5 (very friendly)
b. Fish2:  Name – James, Type – Guppy, Friendliness level – 3 (neutral)

7. Create a method in the driver class called nicestFish that takes in two fish as parameters, compares the friendliness level of  the two fish, and returns the name of the fish with the higher friendliness. Test this method with the fish defined in problem 6. Remember that methods must be created after the end of the main method and called in the main method.
Hint: Fish1.getFriendliness() gives you the integer number of the friendliness of the fish. You have already created getFriendliness() in problem 5.

8. Modify the method nicestFish() to take be a variable argument method that takes in a variable number of fish and returns the nicest fish out of the fish it is given. Hint: Inside of the method, create a new fish called temp. Set temp equal to the first fish passed into the method. Use a for loop to go through all the fish passed into the method and if you discover a fish that is more friendly than temp, set temp equal to that fish. After the for loop is complete, temp should be the friendliest fish. Return temp.

9. Test your method nicestFish() with the fish described in problem 6. Which fish is returned?

10. Determine the best access modifier for each of the following situations:
a. A class Employee records the name, address, salary, and phone number.
b. An adding method inside of a class BasicMath that is also used in the Algebra class.

If you get stuck at any point please email me with your code and questions.

Once you have tested your code submit the following to Canvas:
Your code for both files the Fish class and the FishDriver class.
Screenshot of the output.

 

 

 

 

Homework

Stay Safe

 

What

In this lesson, you will learn how to:
•Recognize the correct general form of a class
• Create an object of a class
• Describe object references
• Create methods that compile with no errors
• Return a value from a method
• Use parameters in a method

 

Why

Class templates are the foundation of the Java language. Understanding how to create a class with various methods will be essential to becoming a good Java programmer. Knowing how to instantiate an object and compare it to other objects is critical to learning to use and work with Java data types.

 

How

By completing the slides, quiz and practice exercises.