Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer the following questions. Please answer all of the questions to rec

ID: 3555662 • Letter: P

Question

Please answer the following questions. Please answer all of the questions to receive the points.

A link to the book to assist with question 4:

http://folk.uib.no/sji093/Objects%20First%20with%20Java%205e.pdf

1. How can you count the number of instances(Objects) created for a certain class? Please explain in details using Java code lines, and in which parts of the class would you add these lines of code to be able to accomplish that.

2. Write a method that uses HashMaps and takes two parameters one of Type String that represents a student ID , and another of type Student, the hashmap should first check if there is an entry for that student in the Hashmap (based on the ID) and if not create an entry to the hashmap associating Students objects with student ID, otherwise the method should Print Out an error indicating that the student already exists in the Hashmap.

3. Write a method in JAVA that uses a switch statement and takes a person's age using an integer parameter , then checks if the age is valid (0-130), and then RETURNS a string based on the age using the following rules:

0-10 Child

11-19 Teenager

20-64 Adult

65- Senior

4. Criticize the first version of "world of Zul" example from Chapter 6, in terms of Design, using and showing that you understand the following terms (responsibility Driven design, cohesion , coupling, refactoring).

Explanation / Answer

******************      Part 1       ********************

You can define a static variable in class. You need to increment it in constructor. Example:

******************      Part 2       ********************

    HashMap<String, Student> myMap;
    public void addNewStudent(String studentId, Student s){
        if(myMap.containsKey(studentId)){
            System.out.println("Error: Student with StudentId="+studentId+" already exists.");
        } else {
            myMap.put(studentId, s);
        }
    }

***************************   Part 3 *************************

    public String getAgeCategory(int age){
        String category="";
        int temp = age/131;
        switch (temp) {
            case 0: System.out.println("This is valid age");
                     break;
            default: System.out.println("This is invalid age");
                    return "invalid age";
        }
      
        if(age >=0 && age <= 10) category = "Child";
        else if(age >=11 && age <= 19) category = "Teenager";
        else if(age >=20 && age <= 64) category = "Adult";
        else if(age >=65) category = "Senior";
        return category;
    }