Announcement

Collapse
No announcement yet.

Anybody mess with java programming?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    #16
    okay, so I need some more help.

    The issue I am having is not so much the actual functioning, it is just the way the instructor wants it.
    The first picture is the instructions from the course page:


    Now here is the text from the book:
    Originally posted by Textbook
    Carpet Calculator
    The Westfield Carpet company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price you multiply the area of the floor (width times length) by the price per square foot of carpet. For example, the area of floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet that costs $8 per square foot would cost $960. (12 x 10 x 8 = 960.)

    First, you should create a class named RoomDimension that has two fields: one for length of the room and one for width. The RoomDimension class should have a method that returns the area of the room.

    Next you should create a RoomCarpet class that has a RoomDesign object as a field it should also have a field for the cost of the carpet per square foot. The RoomCarpet class should have a method that returns the total cost of carpet.

    Figure 6-19 is a UML diagram showing possible class designs and depicting the relationships between the classes. Once you have written these classes, use them in an application that asks the user to enter the dimensions of a room and price per square foot of the desired carpeting. The application should display the total cost of the carpet.
    The next picture is of the UML diagram:



    Now, I am having trouble following the UML diagram to the T. Because the way I read it, I should only have 3 methods in each class. My possible solution is to put additional methods inside the class.
    Gary A.K.A. Carter
    [sig killed by photobucket]

    Comment


      #17
      Now, I think I have the RoomDimension class finished:
      Code:
      /** RoomDimension.java
      Gathers dimensions for a room to calculate area
      To be used with RoomCarpet.java
      
      @author Gary C. Rogers 2010
      */
      
      
      public class RoomDimension {
      	
      	private double length;
      	private double width;
      	
      	//This method allows another class to mutate length and width
      	public double[] RoomDimension(double len, double wid)
      	{
      
      		length = len;
      		width = wid;
      		return new double[] {length, width};		
      	}
      	
      	//This method returns the area of the room
      	public double getArea()
      	{
      		return length * width;
      	}
      }
      Gary A.K.A. Carter
      [sig killed by photobucket]

      Comment


        #18
        k, so I would very, very strongly recommend NOT copying the code on this page, but I pretty much found the assignment based on the .java file names...

        http://www.dreamincode.net/forums/to...tructor-error/

        ^That's for carpetcalc.java

        http://www.dreamincode.net/forums/to...s-a-parameter/

        ^roomcarpet.java and roomdimension.java

        There's nothing wrong with referencing the code, but it's very easy for instructors to do the same thing I did, so write your own code and take pointers from these sites.

        Welcome to my method for a C+ grade in C++ programming
        Fix your Computer!

        Originally posted by MikeW
        D, I'm a fanciful motherfucker. My ish is clean, quick, plush, mature and sophisticated.

        ┌─┐
        ┴─┴
        ಠ_ರೃ

        Comment


          #19
          Originally posted by Masamune View Post
          k, so I would very, very strongly recommend NOT copying the code on this page, but I pretty much found the assignment based on the .java file names...

          http://www.dreamincode.net/forums/to...tructor-error/

          ^That's for carpetcalc.java

          http://www.dreamincode.net/forums/to...s-a-parameter/

          ^roomcarpet.java and roomdimension.java

          There's nothing wrong with referencing the code, but it's very easy for instructors to do the same thing I did, so write your own code and take pointers from these sites.

          Welcome to my method for a C+ grade in C++ programming
          That would work from the standpoint of calculating the area and stuff, but it does not follow the UML diagram from the book.
          Gary A.K.A. Carter
          [sig killed by photobucket]

          Comment


            #20
            From your posted instructions:

            Originally posted by Textbook
            Figure 6-19 is a UML diagram showing possible class designs and depicting the relationships between the classes. Once you have written these classes, use them in an application that asks the user to enter the dimensions of a room and price per square foot of the desired carpeting. The application should display the total cost of the carpet.
            I think the instructions show that you don't have to follow the UML diagram exactly. It's more or less a proposal of how to get the program built. If your solution is to add more methods to your classes then as long as everything works you should be fine.
            Fix your Computer!

            Originally posted by MikeW
            D, I'm a fanciful motherfucker. My ish is clean, quick, plush, mature and sophisticated.

            ┌─┐
            ┴─┴
            ಠ_ರೃ

            Comment


              #21
              Originally posted by Masamune View Post
              From your posted instructions:



              I think the instructions show that you don't have to follow the UML diagram exactly. It's more or less a proposal of how to get the program built. If your solution is to add more methods to your classes then as long as everything works you should be fine.
              Yeah. I think I have it now for the most part. Still trying to figure out the value of putting the toStrings in there.
              Gary A.K.A. Carter
              [sig killed by photobucket]

              Comment


                #22
                And I figured out the toString method. Now I don't have any assignments due until 3 August.
                Gary A.K.A. Carter
                [sig killed by photobucket]

                Comment


                  #23
                  I have another issue. This bit of code is stuck in the while loop, and I can't figure out why.
                  Code:
                  import java.util.Scanner;
                  
                  public class Tournament
                  {
                  	public static void main(String[] args)
                  	{
                  		int teamCount;
                  		int teamWins;
                  		String name;
                  		
                  		Scanner input = new Scanner(System.in);
                  		//Ask the user for thr number of teams for the tournament
                  		System.out.println("What is the number of teams in the tournament?");
                  		teamCount = input.nextInt();
                  		
                  		//Make sure that the input is valid for matchups
                  		while (teamCount !=4 || teamCount !=8 || teamCount !=16 || teamCount !=32 || teamCount !=64)
                  		{
                  			System.out.println("You have entered an invalid number of teams.  Please try again.");
                  			
                  			System.out.println("What is the number of teams in the tournament?");
                  			teamCount = input.nextInt();
                  		}
                  
                  		//Declares the array to store objects
                  		Team[] teamArray = new Team[teamCount];
                  		int count = 0;
                  			
                  		//Loop allows for the input for the team objects
                  		do
                  		{
                  			System.out.println("What is the team name?");
                  			name = input.nextLine();
                  				
                  			System.out.println("How many wins do they have?");
                  			teamWins = input.nextInt();
                  				
                  			teamArray[count]= new Team( name, teamWins);
                  			count++;
                  				
                  			if (count < teamCount)
                  			{
                  				System.out.println("Team information gathered.  Input next team.");
                  			}
                  				
                  			else
                  			{
                  				System.out.println("All teams have been inputed.  Thank you.");
                  			}
                  		}
                  			
                  		while (count <= teamCount);
                  			
                  		for (int index = 0; index < teamArray.length; index++)
                  		{
                  				
                  			System.out.println(teamArray[index].getTeamName() + "" + teamArray[index].getWins());
                  				
                  		}
                  				
                  		
                  		
                  	}
                  }
                  If I enter 4, 8, 16, 32, or 64 it should exit the while loop, but it doesn't and I can't figure it out.
                  Gary A.K.A. Carter
                  [sig killed by photobucket]

                  Comment


                    #24
                    Okay, I just played with that code a bit.

                    If I use this statement:
                    Code:
                    while (teamCount!=4)
                    It works. But as soon as I put an or operand || it doesn't work. It is stuck infinitely.
                    Gary A.K.A. Carter
                    [sig killed by photobucket]

                    Comment


                      #25
                      Got it. If you need to set up a tournament, this will help, lol.

                      Code:
                      /* Tournament.java
                      This program takes user input of teams and number of wins to create
                      an array of team objects and makes pairings of teams for a tournament
                      @author Gary C. Rogers 2010
                      */
                      
                      import java.util.Scanner;
                      
                      public class Tournament
                      {
                      	public static void main(String[] args)
                      	{
                      		int teamCount;
                      		int teamWins;
                      		String name;
                      		
                      		Scanner input = new Scanner(System.in);
                      		//Ask the user for thr number of teams for the tournament
                      		System.out.println("What is the number of teams in the tournament?");
                      		teamCount = input.nextInt();
                      		
                      		//Make sure that the input is valid for matchups
                      		while (teamCount!=4 && teamCount !=8 && teamCount !=16 && teamCount !=32 && teamCount !=64)
                      		{
                      			System.out.println("You have entered an invalid number of teams.  Please try again.");
                      			
                      			System.out.println("What is the number of teams in the tournament?");
                      			teamCount = input.nextInt();
                      		}
                      
                      		//Declares the array to store objects
                      		Team[] teamArray = new Team[teamCount];
                      		int count = 0;
                      		
                      		System.out.println("Input the teams in order of most wins to least.");
                      			
                      		//Loop allows for the input for the team objects
                      		do
                      		{
                      			//Gets the team name from the user
                      			System.out.println("What is the team name?");
                      			name = input.next();
                      			//Asks the user how many wins the team has	
                      			System.out.println("How many wins do they have?");
                      			teamWins = input.nextInt();
                      			
                      			//Stores the information into the array of team objects	
                      			teamArray[count]= new Team( name, teamWins);
                      			count++;
                      				
                      			if (count < teamCount)
                      			{
                      				//Prompts the user to input another team
                      				System.out.println("Team information gathered.  Input next team.");
                      			}
                      				
                      			else
                      			{
                      				//Tells user that all the teams have been put into the system
                      				System.out.println("All teams have been inputed.  Thank you.");
                      				System.out.println("");
                      			}
                      		}
                      			
                      		while (count < teamCount);
                      		
                      		//Loop interates over and over to print the names and wins of each of the team objects	
                      		for (int index = 0; index < teamArray.length; index++)
                      		{
                      			//Prints the team name and wins in one line	
                      			System.out.println(teamArray[index].getTeamName() + " " + teamArray[index].getWins());
                      			
                      				
                      		}
                      		//This code starts the team match ups
                      		System.out.println("");
                      		System.out.println("Team match ups:");
                      		
                      		//This prints the game match ups if there are 4 teams
                      		if (teamCount == 4)
                      		{
                      			
                      			System.out.println("Game 1: " + teamArray[0].getTeamName() + " vs. " + teamArray[3].getTeamName());
                      			System.out.println("Game 2: " + teamArray[1].getTeamName() + " vs. " + teamArray[2].getTeamName());
                      			
                      		}
                      		
                      		//This prints the game match ups if there are 8 teams
                      		else if (teamCount == 8)
                      		{
                      			
                      			System.out.println("Game 1: " + teamArray[0].getTeamName() + " vs. " + teamArray[7].getTeamName());
                      			System.out.println("Game 2: " + teamArray[1].getTeamName() + " vs. " + teamArray[6].getTeamName());
                      			System.out.println("Game 3: " + teamArray[2].getTeamName() + " vs. " + teamArray[5].getTeamName());
                      			System.out.println("Game 4: " + teamArray[3].getTeamName() + " vs. " + teamArray[4].getTeamName());
                      			
                      		}
                      		
                      		//This prints the team match ups if there are 16 teams
                      		else if (teamCount == 16)
                      		{
                      			System.out.println("Game 1: " + teamArray[0].getTeamName() + " vs. " + teamArray[15].getTeamName());
                      			System.out.println("Game 2: " + teamArray[1].getTeamName() + " vs. " + teamArray[14].getTeamName());
                      			System.out.println("Game 3: " + teamArray[2].getTeamName() + " vs. " + teamArray[13].getTeamName());
                      			System.out.println("Game 4: " + teamArray[3].getTeamName() + " vs. " + teamArray[12].getTeamName());
                      			System.out.println("Game 5: " + teamArray[4].getTeamName() + " vs. " + teamArray[11].getTeamName());
                      			System.out.println("Game 6: " + teamArray[5].getTeamName() + " vs. " + teamArray[10].getTeamName());
                      			System.out.println("Game 7: " + teamArray[6].getTeamName() + " vs. " + teamArray[9].getTeamName());
                      			System.out.println("Game 8: " + teamArray[7].getTeamName() + " vs. " + teamArray[8].getTeamName());
                      		}
                      		
                      		//This prints the team match ups if there are 32 teams
                      		else if (teamCount == 32)
                      		{
                      			System.out.println("Game 1: " + teamArray[0].getTeamName() + " vs. " + teamArray[31].getTeamName());
                      			System.out.println("Game 2: " + teamArray[1].getTeamName() + " vs. " + teamArray[30].getTeamName());
                      			System.out.println("Game 3: " + teamArray[2].getTeamName() + " vs. " + teamArray[29].getTeamName());
                      			System.out.println("Game 4: " + teamArray[3].getTeamName() + " vs. " + teamArray[28].getTeamName());
                      			System.out.println("Game 5: " + teamArray[4].getTeamName() + " vs. " + teamArray[27].getTeamName());
                      			System.out.println("Game 6: " + teamArray[5].getTeamName() + " vs. " + teamArray[26].getTeamName());
                      			System.out.println("Game 7: " + teamArray[6].getTeamName() + " vs. " + teamArray[25].getTeamName());
                      			System.out.println("Game 8: " + teamArray[7].getTeamName() + " vs. " + teamArray[24].getTeamName());
                      			System.out.println("Game 9: " + teamArray[8].getTeamName() + " vs. " + teamArray[23].getTeamName());
                      			System.out.println("Game 10: " + teamArray[9].getTeamName() + " vs. " + teamArray[22].getTeamName());
                      			System.out.println("Game 11: " + teamArray[10].getTeamName() + " vs. " + teamArray[21].getTeamName());
                      			System.out.println("Game 12: " + teamArray[11].getTeamName() + " vs. " + teamArray[20].getTeamName());
                      			System.out.println("Game 13: " + teamArray[12].getTeamName() + " vs. " + teamArray[19].getTeamName());
                      			System.out.println("Game 14: " + teamArray[13].getTeamName() + " vs. " + teamArray[18].getTeamName());
                      			System.out.println("Game 15: " + teamArray[14].getTeamName() + " vs. " + teamArray[17].getTeamName());
                      			System.out.println("Game 16: " + teamArray[15].getTeamName() + " vs. " + teamArray[16].getTeamName());
                      		}
                      		
                      		//This prints the team match ups if there are 64 teams
                      		else if (teamCount == 64)
                      		{
                      			System.out.println("Game 1: " + teamArray[0].getTeamName() + " vs. " + teamArray[63].getTeamName());
                      			System.out.println("Game 2: " + teamArray[1].getTeamName() + " vs. " + teamArray[62].getTeamName());
                      			System.out.println("Game 3: " + teamArray[2].getTeamName() + " vs. " + teamArray[61].getTeamName());
                      			System.out.println("Game 4: " + teamArray[3].getTeamName() + " vs. " + teamArray[60].getTeamName());
                      			System.out.println("Game 5: " + teamArray[4].getTeamName() + " vs. " + teamArray[59].getTeamName());
                      			System.out.println("Game 6: " + teamArray[5].getTeamName() + " vs. " + teamArray[58].getTeamName());
                      			System.out.println("Game 7: " + teamArray[6].getTeamName() + " vs. " + teamArray[57].getTeamName());
                      			System.out.println("Game 8: " + teamArray[7].getTeamName() + " vs. " + teamArray[56].getTeamName());
                      			System.out.println("Game 9: " + teamArray[8].getTeamName() + " vs. " + teamArray[55].getTeamName());
                      			System.out.println("Game 10: " + teamArray[9].getTeamName() + " vs. " + teamArray[54].getTeamName());
                      			System.out.println("Game 11: " + teamArray[10].getTeamName() + " vs. " + teamArray[53].getTeamName());
                      			System.out.println("Game 12: " + teamArray[11].getTeamName() + " vs. " + teamArray[52].getTeamName());
                      			System.out.println("Game 13: " + teamArray[12].getTeamName() + " vs. " + teamArray[51].getTeamName());
                      			System.out.println("Game 14: " + teamArray[13].getTeamName() + " vs. " + teamArray[50].getTeamName());
                      			System.out.println("Game 15: " + teamArray[14].getTeamName() + " vs. " + teamArray[49].getTeamName());
                      			System.out.println("Game 16: " + teamArray[15].getTeamName() + " vs. " + teamArray[48].getTeamName());
                      			System.out.println("Game 17: " + teamArray[16].getTeamName() + " vs. " + teamArray[47].getTeamName());
                      			System.out.println("Game 18: " + teamArray[17].getTeamName() + " vs. " + teamArray[46].getTeamName());
                      			System.out.println("Game 19: " + teamArray[18].getTeamName() + " vs. " + teamArray[45].getTeamName());
                      			System.out.println("Game 20: " + teamArray[19].getTeamName() + " vs. " + teamArray[44].getTeamName());
                      			System.out.println("Game 21: " + teamArray[20].getTeamName() + " vs. " + teamArray[43].getTeamName());
                      			System.out.println("Game 22: " + teamArray[21].getTeamName() + " vs. " + teamArray[42].getTeamName());
                      			System.out.println("Game 23: " + teamArray[22].getTeamName() + " vs. " + teamArray[41].getTeamName());
                      			System.out.println("Game 24: " + teamArray[23].getTeamName() + " vs. " + teamArray[40].getTeamName());
                      			System.out.println("Game 25: " + teamArray[24].getTeamName() + " vs. " + teamArray[39].getTeamName());
                      			System.out.println("Game 26: " + teamArray[25].getTeamName() + " vs. " + teamArray[38].getTeamName());
                      			System.out.println("Game 27: " + teamArray[26].getTeamName() + " vs. " + teamArray[37].getTeamName());
                      			System.out.println("Game 28: " + teamArray[27].getTeamName() + " vs. " + teamArray[36].getTeamName());
                      			System.out.println("Game 29: " + teamArray[28].getTeamName() + " vs. " + teamArray[35].getTeamName());
                      			System.out.println("Game 30: " + teamArray[29].getTeamName() + " vs. " + teamArray[34].getTeamName());
                      			System.out.println("Game 31: " + teamArray[30].getTeamName() + " vs. " + teamArray[33].getTeamName());
                      			System.out.println("Game 32: " + teamArray[31].getTeamName() + " vs. " + teamArray[32].getTeamName());
                      		}
                      		
                      		//This is another layer of redundancy so that if for some reason the check for number of teams did not work before the user will get an error
                      		else
                      		{
                      			System.out.println("Somehow the number of teams got messed up and passed the first filter.  Not good.");
                      		}
                      			
                      	
                      			
                      		
                      			
                      
                      
                      			
                      	}
                      }
                      You also need this .java to run it

                      Code:
                      /* Team.java
                      Class that creates a team.  To be used with Tournament.java
                      @author Gary C. Rogers 2010
                      */
                      
                      public class Team
                      {
                      	private String teamName;
                      	private int wins;
                      	
                      	//Constructor for instance fields
                      	public Team(String teamName, int wins)
                      	{
                      		this.teamName =  teamName;
                      		this.wins = wins;
                      	}
                      	
                      	//Returns the team name
                      	public String getTeamName()
                      	{
                      		return teamName;
                      	}
                      	
                      	//Returns the number of wins	
                      	public int getWins()
                      	{
                      		return wins;
                      	}
                      }
                      Gary A.K.A. Carter
                      [sig killed by photobucket]

                      Comment

                      Working...
                      X