« All Events
9 AM – Indo – Intro to Java – Joel
February 22 @ 9:00 am - 10:00 am
T0day we did:
- We reviewed the practice test.
Homework:
- Make sure you study these topics for the first quiz:
- Variable and Data types
- If, Else if, Else (OR and AND)
- List and For-each loop
- For loop using index
- While loop
- Random module
- Java methods (don’t forget constructors, getters and setters too) and classes
- Hashmap
- Fix Java errors
- Remember, the quiz is open notes (you can look at previous files, but no external sources like Google or ChatGPT as cheating will result in a 0). The duration is 1 hour and you can copy and paste your code from Intellij to the Google form for your answers.
- Here is the rest of the explanation for number 11 in the practice quiz (all the files we did today are in the Google Drive):
- In the initializeAnimals, we create 2 animals and put them in the map.
- In the run method, we check if the user enters “quit” using the id variable, and if they did, then we quit the while true loop.
- Otherwise, we take the just take the animal (value) using the id (key). Remember, in hashmaps we simply take it using .get so we can check if it exists (or is not null) later.
- We check if the animal is null (doesn’t exist). If it is, we print this animal id does not exist in our system.
- Otherwise, using the animal we took, we call the getAnimalInfo which prints all the information.
- Don’t forget to call both the initializeAnimals and run inside the public static void main.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class AnimalSystem {
Map<String, Animal> animalMap = new HashMap<>();
Scanner s = new Scanner(System.in);
public void initializeAnimals(){
Animal a1 = new Animal("Joey", "Kangaroo", 3, "123");
Animal a2 = new Animal("Brody", "Dog", 8, "789");
animalMap.put("123", a1);
animalMap.put("789", a2);
}
public void run(){
while (true){
System.out.print("Enter an animal id: ");
String id = s.nextLine();
if (id.equalsIgnoreCase("quit")){
break;
}
Animal animal = animalMap.get(id);
if (animal == null){
System.out.println("This animal id does not exist in our system");
} else {
System.out.println(animal.getAnimalInfo());
}
}
}
public static void main (String []args){
AnimalSystem main = new AnimalSystem();
main.initializeAnimals();
main.run();
}
}