Hi all,
I am a freshman in college and and (currently) a Comp. Science major. I was trying to do some practice questions (not for homework credit) from my java textbook, so that I would be familar with these types of code(s), for class tomorrow afternoon.
I think I pretty much have the first one done, but I'm not really sure. The second and third ones I attempted, but am a little confused about what I am still missing. If someone could give me some tips and possibly show me what the correct code might look like, I would greatly appreciate it (see practice questions below):
Thanks,
-RBC.
-----------------------------
Code 1 (State.java and TestState.java:
11. Create a class named State that hold the following fields: a String for the name of the state, an integer for the population, and two City objects that hold data about the capital city and most populous city. The State constructor requires six parameters that represent the names and populations of the state, its capital, and its most populous city. Provide get methods for each field. Create the City class to be nonstatic, private inner class within the State class; the City class contains a city's name and population. Create a class to assign values to and display values from two State objects. Save the files as State.java and TestState.java
My code:
State.java:
public class State { String state = “South Carolina”; int population = 4,625,364; City.object1 = new City(cityCapital,capitalPopulation) City.object2 = new City(cityLargest,largestPopulation) public State(){ state = "South Carolina"; capitalPopulation = 200,452; largestPopulation = 450,726 population = 4,625,364 cityCapital = Columbia cityLargest = Charleston } { public State(String state, int population){ this.state = state; this.population = population; } public int getState() { return state; } public int getPopulation() { return population; } public void display() { System.out.println(“The state is: ” + state “.” + “The population is: “ + population “.”); } private class City { private String cityCapital; private int capitalPopulation; public City(String cityCapital, int capitalPopulation) { capitalPopulation = 200,452 cityCapital = Columbia } }
Code 2 (BloodData.java and Patient.java:
4. a. Create a class named BloodData that includes fields that hold a blood type (the four blood types are O, A, B, and AB) and an Rh factor (the factors are + and -). Create a default constructor that sets the fields to "O" and "+", and an overloaded constructor that requires values for both fields. Include get and set methods for each field. Save this file as BloodData.java. Create an application named TestBloodData that demonstrates that each method works correctly. Save the application as TestBloodData.java.
b. Create a class named Patient that includes an ID number, age, and BloodData. Provide a default constructor that sets the ID number to "0", the age to 0, and the BloodData to "O" and "+". Create an overloaded constructor that provides values for each field. Also provide get methods for each field. Save the file as Patient.java. Create an application named TestPatient that demonstrates that each method works correctly, and save it as TestPatient.java.
My code:
public class BloodData { String bloodType1 = “O”; String bloodType2 = “AB”; String bloodType3 = “B”; String bloodType4 = “A”; String rhPositive = “+” String rhNegative = “-” public BloodData(){ bloodType1 = “O”; rhPositive = “+”; } public void display(){ System.out.println("The blood data is " + rh + " bloodType "." ); } public BloodData(String rh, String bloodType){ } public int getBloodType1() { return bloodType1; } public int getBloodType2() { return bloodType2; } public int getBloodType1() { return bloodType1; } public int getBloodType3() { return bloodType3; } public int getBloodType4() { return bloodType4; } } }
Code 3 (Tip Top Bakery:
5. a. Create a class for the Tip Top Bakery named Bread with data fields for bread type (such as "rye”) and calories per slice. Include a constructor that takes parameters for each field, and include get methods that return the values of the fields. Also include a public final static String named MOTTO and initialize it to The staff of life. Write an application named TestBread to instantiate three Bread objects with different values, and then display all the data, including the motto, for each object. Save both the Bread.java and TestBread.java files.
b. Create a class named SandwichFilling. Include a field for the filling type (such as “egg salad”) and another for the calories in a serving. Include a constructor that takes parameters for each field, and include get methods that return the values of the fields. Write an application named TestSandwichFilling to instantiate three SandwichFilling objects with different values, and then display all the data for each object. Save both the SandwichFilling.java and TestSandwichFilling.java files.
c. Create a class named Sandwich. Include a Bread field and a SandwichFilling field. Include a constructor that takes parameters for each field needed in the two objects and assigns them to each object’s constructor. Write an application named TestSandwich to instantiate three Sandwich objects with different values, and then display all the data for each object, including the total calories in a Sandwich, assuming that each Sandwich is made using two slices of Bread. Save both the Sandwich.java and TestSandwich.java files.