Question

In: Computer Science

Java: modify the given example code to make it possible to create a student object by...

Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.  

import java.util.HashMap;
import java.util.Map;

public class Student
{
   private final String aName;
   private String aGender;
   private int aAge;
   private Country aCountry;
   private Map aTags = new HashMap<>();
   
   public Student(String pName)
   {
      aName = pName;
   }
   
   public String getName()
   {
      return aName;
   }
   //getGender, getAge, getCountry, getTag

   public void editTag(String pTag, String pValue)
   {
      aTags.put(pTag, pValue);
   }
}

Solutions

Expert Solution

creating student with name JACK and no gender and age. except name, all fields are optional

Code with comments

Temp.js (driver class)

import java.util.HashMap;

import java.util.Map;

import java.util.Optional;

class Temp {

    public static void main(String[] args) {

        Student stud = new Student("JACK");

        System.out.println(stud.getName());

        System.out.println(stud.getGender());

        System.out.println(stud.getAge());

    }

}

class Country {

}// define your class

class Student {

    private final String aName;

    private Optional<String> aGender; // optional fields

    private Optional<Integer> aAge; // optional fields

    private Optional<Country> aCountry; // optional fields

    private Optional<Map> aTags; // optional fields

    public Student(String pName) {

        aName = pName;

        aGender = Optional.ofNullable(null); // setting null

        aAge = Optional.ofNullable(null); // setting null

        aCountry = Optional.ofNullable(null); // setting null

        aTags = Optional.of(new HashMap<>()); // initializing with empty map

    }

    public String getName() {

        return aName; // return name

    }

    // getGender, getAge, getCountry, getTag

    public void editTag(String pTag, String pValue) {

        aTags.get().put(pTag, pValue);

    }

    public String getGender() {

        return aGender.orElse("Unknown Gender"); // return if present otherwise this parameter

    }

    public Integer getAge() {

        return aAge.orElse(-1);// return if present otherwise this parameter

    }

    public Country getCountry() {

        return aCountry.orElse(null);// return if present otherwise this parameter

    }

    public Map getTags() {

        return aTags.get();

    }

}


Related Solutions

Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Modify the object provided in the code below to include a variety of overloaded operators in...
Modify the object provided in the code below to include a variety of overloaded operators in C++. Your object should have the following member variables: 1. float *arr a. A dynamic float array that constitutes the data in your myArray object. 2. int size a. The size of the array that the myArray object holds Modify the provided code to include a variety of overloaded operators Operators to overload: 1. bool operator!=(myArray& obj2) a. Tests to see if the calling...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
Need to make Java code as following: Create a dice playing threading program to do the...
Need to make Java code as following: Create a dice playing threading program to do the following: 1. Start a thread for a process to simulate a computer rolling a die 1000 times. 2. Start another thread for a process to simulate a user rolling a die 1000 times. 3. Keep track of rolls for user and computer in an array(s). 4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends. 5. Display...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Modify the following java code, utilizing a loop mechanism to enable the user to use the...
Modify the following java code, utilizing a loop mechanism to enable the user to use the calculator more than once. The program does the following:    It prompts the user to enter 2 numbers.    It prompts the user to choose an operation to perform on those numbers:    Operation 1: Addition.    Operation 2: Subtraction.    Operation 3: Multiplication.    Operation 4: Division.    It outputs the result of the operation.    It asks the user if they want...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message...
Java: Make 3 use cases for the code bellow. Example of Use case: Leave a Message 1.Caller dials main number of voice mail system 2.System speaks prompt 3.User types extension number 4.System speaks 5.Caller speaks message Variation #1 3.1 In step 3, user enters invalid extension number 3.2 Voice mail system speaks 3.3 Continue with step 2. What the code Does: adds a new regular task, delete a task , show all tasks, and show regular tasks. my code: import...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT