Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with My Codes!

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with My Codes!

    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.
    Last edited by pacerboy9; September 28th, 2014 at 03:06 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help with My Codes!

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    When you say that you're confused what to do next, describe specifically what you're unsure about or what you need help with. Ask specific questions. We're not sure what you mean by "what to do next."

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with My Codes!

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    When you say that you're confused what to do next, describe specifically what you're unsure about or what you need help with. Ask specific questions. We're not sure what you mean by "what to do next."
    I fixed my post, and restated that sentence.

Similar Threads

  1. Need Help and Codes im a new bee
    By Channelle in forum Member Introductions
    Replies: 1
    Last Post: October 5th, 2013, 12:15 PM
  2. Help for my codes
    By noobie in forum What's Wrong With My Code?
    Replies: 28
    Last Post: March 21st, 2013, 10:31 AM
  3. Help me please with my codes
    By sara92 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 12th, 2012, 11:52 AM
  4. Looking for help on two codes:
    By captianjaax in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 20th, 2011, 11:49 PM
  5. Please Help What's Wrong with my Codes!
    By bertzki10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2011, 11:03 AM