Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    can you edit my code and show me what you mean. Im having trouble implementing and understanding the changes you are telling me to make.
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    I'm surprised that it actually compiled.

    You have to make sure all the code blocks are in the appropriated function.
    1. regarding setRep, you should be able to more or less copy my code into that function.
    Function if 1 and 10 should be "selectable" too:
    Code:
    	public boolean setRep(int r)
    	{
    		if(r >= Low && r <= High)
    		{
    			Rep = r;
    			return false;
    		}
    		else 
    		{
    			// optional to add a print out telling the input is outside the allowed boundaries
    			return true;
    		}
    	}
    2. You should not call the scanner, the user are asked for input in the main function
    3. The block with even/odd and the number switch should be placed inside a function called toString().
    4. You should not printout the lines, but instead return the combined result as a concatenated string.
    5. You are referring the variable r outside setRep; you should instead use the class name called Rep.
    Ive tried separating things into the function toString but keep running into problems. Where does that function get referenced? What do you mean about my references to r outside setRep. Can you show me the changes you are suggesting in the code I provided before?
  4. #18
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,673
    Rep Power
    1958
    If you have made some changes, i suggest you post it. Then I can take a look at it.

    For official explanation, you can take a look at the documentation
    You could say toString() is used to to return the "state" of the class as a string.
    It is called indirectly in below call, where it want to print out the box object:
    Code:
    System.out.println( box );
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    If you have made some changes, i suggest you post it. Then I can take a look at it.

    For official explanation, you can take a look at the documentation
    You could say toString() is used to to return the "state" of the class as a string.
    It is called indirectly in below call, where it want to print out the box object:
    Code:
    System.out.println( box );
    How is it called by the box object? the toString is never referenced anywhere? I cant quite understand what you are describing? Below are the changes I made to the code. Can you edit it and show me what you mean?

    PHP Code:
    package box.application;  
    /** 
     * 
     * @author Alex Kramer 
     */ 
    public class Box 
         
        private 
    int HighLow 
        private 
    int Rep
      
         
        public 
    Box(){ 
         
    High 10 
         
    Low 
        } 
        public 
    Box (int h,int L) { 
            
    High h
            
    Low L
        } 
         
        public 
    void setHigh(int h){ 
            
    High h
        } 
        public 
    void setLow(int L){ 
            
    Low L
        } 
         
    /** 
         * 
         * @param 
         */ 

    /** 
         * 
         * @return 
         */ 
        
    public int getHigh() { 

            return 
    High
        } 
        public 
    int getLow() { 

            return 
    Low
        } 
      public 
    boolean setRep(int r)
        {
        if( 
    Rep >= Low && Rep <= High)
            {
            
    Rep r;
            return 
    false;
            }
        else 
            {
            
    System.out.println("Outside system Constraints");
            return 
    true;
            }
        }
      
             

    class 
    toString{
       
        private 
    int Rep;
                
     
        
        
    String EvenOdd
        {if(
    Rep%== 0) {EvenOdd "Even"
        } 
        else {
    EvenOdd "Odd";} 
         
        {   
    int number Rep
            
    String numberString "one";  

            
            switch (
    number) { 
                case 
    1:  numberString "one"
                         break; 
                case 
    2:  numberString "two"
                         break; 
                case 
    3:  numberString "three"
                         break; 
                case 
    4:  numberString "four"
                         break; 
                case 
    5:  numberString "five"
                         break; 
                case 
    6:  numberString "six"
                         break; 
                case 
    7:  numberString "seven"
                         break; 
                case 
    8:  numberString "eight"
                         break; 
                case 
    9:  numberString "nine"
                         break; 
                case 
    10numberString "ten"
                         break; 
            
            } 
            
    int n=0
            while (
    Rep){ 
                 
            
    System.out.println("A " numberString " was enetered, and it is " EvenOdd); 
            
    n++; 
            } 
        }
    }}

  8. #20
  9. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,673
    Rep Power
    1958
    You are almost there

    Did you look at the link I provide?
    toString() is a function similar to getRep().

    Instead of
    Code:
    class toString{
    you want to use
    Code:
    public String toString() {

    toString() is a special function that will be called by the class itself as standard. Alternative, the code could look like this:
    Code:
    System.out.println( box.toString() );
    You should also remove the Rep definition in toString
    Code:
    private int Rep;
    as it is already defined at the top of the Box class.

    What editor do you use for coding?
    You should try to align the opening and closing brackets { } to make it easier to read.

    The end result of toString will be like this:
    java Code:
     
    	public String toString()
    	{
    		String EvenOdd;
     
    		if(rep%2 == 0) 
    		{
    			EvenOdd = "Even";
    		}
    		else 
    		{
    			EvenOdd = "Odd";
    		}
     
     
    		String numberString = ""; 
     
     
    		switch (rep) {
    			case 1:  numberString = "one";
    				break;
    			case 2:  numberString = "two";
    				break;
    			case 3:  numberString = "three";
    				break;
    			case 4:  numberString = "four";
    				break;
    			case 5:  numberString = "five";
    				break;
    			case 6:  numberString = "six";
    				break;
    			case 7:  numberString = "seven";
    				break;
    			case 8:  numberString = "eight";
    				break;
    			case 9:  numberString = "nine";
    				break;
    			case 10: numberString = "ten";
    				break;
    		}
     
    		int n=0;
    		String result = "";
    		while (n < rep)
    		{			
    			result += "A " + numberString + " was entered, and it is " + EvenOdd + '\n';
    			n++;
    		}
    		return result;
    	}


    To follow the "standard" coding practice, you should add a default case in the number switch.
    Last edited by MrFujin; May 27th, 2013 at 06:07 PM.
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    This is what I have

    PHP Code:
    package box.application;  
    /** 
     * 
     * @author Alex Kramer 
     */ 
    public class Box 
         
        private 
    int HighLow 
        private 
    int Rep
      
         
        public 
    Box(){ 
         
    High 10 
         
    Low 
        } 
        public 
    Box (int h,int L) { 
            
    High h
            
    Low L
        } 
         
        public 
    void setHigh(int h){ 
            
    High h
        } 
        public 
    void setLow(int L){ 
            
    Low L
        } 
         
    /** 
         * 
         * @param 
         */ 

    /** 
         * 
         * @return 
         */ 
        
    public int getHigh() { 

            return 
    High
        } 
        public 
    int getLow() { 

            return 
    Low
        } 
      public 
    boolean setRep(int r)
        {
        if( 
    Rep >= Low && Rep <= High)
            {
            
    Rep r;
            return 
    false;
            }
        else 
            {
            
    System.out.println("Outside system Constraints");
            return 
    true;
            }
        }
      
             

        
    /**
         *
         * @param Rep
         * @return
         */
        
    public String toString() {       
     
        
        
    String EvenOdd
        {if(
    Rep%== 0
        {
            
    EvenOdd "Even"
        } 
        else 
        {
            
    EvenOdd "Odd";
        } 
         
        
            
    String numberString "one";  

            
            switch (
    Rep) { 
                case 
    1:  numberString "one"
                         break; 
                case 
    2:  numberString "two"
                         break; 
                case 
    3:  numberString "three"
                         break; 
                case 
    4:  numberString "four"
                         break; 
                case 
    5:  numberString "five"
                         break; 
                case 
    6:  numberString "six"
                         break; 
                case 
    7:  numberString "seven"
                         break; 
                case 
    8:  numberString "eight"
                         break; 
                case 
    9:  numberString "nine"
                         break; 
                case 
    10numberString "ten"
                         break; 
            
            } 
            
    int n=0
            
    String result "";
            while (
    Rep){ 
                 
            
    System.out.println("A " numberString " was enetered, and it is " EvenOdd); 
            
    n++; 
            }
            return 
    result;
        }
        }

    But I keep getting this output, how can I fix this?
    Code:
    run:
    Enter the number of repetitions (between 1 and 10 ): 2
    Outside system Constraints
    Enter the number of repetitions (between 1 and 10 ): 6
    Outside system Constraints
    Enter the number of repetitions (between 1 and 10 ): 11
    Outside system Constraints
    Enter the number of repetitions (between 1 and 10 ):
  12. #22
  13. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,673
    Rep Power
    1958
    Look at how the Box is created:
    Code:
    Box box = new Box(1,10);
    And you constructor:
    Code:
    public Box (int h,int L) {
    The first number is the lowest number.
  14. #23
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    Look at how the Box is created:
    Code:
    Box box = new Box(1,10);
    And you constructor:
    Code:
    public Box (int h,int L) {
    The first number is the lowest number.
    I switched the L and h but i am still getting the same problem as before. It is reading outside the constraints? Do i need to change more of the basic constructor?
  16. #24
  17. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,673
    Rep Power
    1958
    the mistakes is in setRep, you compare Low and High with Rep:
    Code:
    if( Rep >= Low && Rep <= High)
    Rep is not assigned a value yet, the validation should be with the parameter r (which is the value entered by the user):
    Code:
    if( r >= Low && r <= High)
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    You are almost there

    Did you look at the link I provide?
    toString() is a function similar to getRep().

    Instead of
    Code:
    class toString{
    you want to use
    Code:
    public String toString() {

    toString() is a special function that will be called by the class itself as standard. Alternative, the code could look like this:
    Code:
    System.out.println( box.toString() );
    You should also remove the Rep definition in toString
    Code:
    private int Rep;
    as it is already defined at the top of the Box class.

    What editor do you use for coding?
    You should try to align the opening and closing brackets { } to make it easier to read.

    The end result of toString will be like this:
    java Code:
     
    	public String toString()
    	{
    		String EvenOdd;
     
    		if(rep%2 == 0) 
    		{
    			EvenOdd = "Even";
    		}
    		else 
    		{
    			EvenOdd = "Odd";
    		}
     
     
    		String numberString = ""; 
     
     
    		switch (rep) {
    			case 1:  numberString = "one";
    				break;
    			case 2:  numberString = "two";
    				break;
    			case 3:  numberString = "three";
    				break;
    			case 4:  numberString = "four";
    				break;
    			case 5:  numberString = "five";
    				break;
    			case 6:  numberString = "six";
    				break;
    			case 7:  numberString = "seven";
    				break;
    			case 8:  numberString = "eight";
    				break;
    			case 9:  numberString = "nine";
    				break;
    			case 10: numberString = "ten";
    				break;
    		}
     
    		int n=0;
    		String result = "";
    		while (n < rep)
    		{			
    			result += "A " + numberString + " was entered, and it is " + EvenOdd + '\n';
    			n++;
    		}
    		return result;
    	}


    To follow the "standard" coding practice, you should add a default case in the number switch.
    Do I need a setRep line in my code? I thought it was unnecessary?
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    the mistakes is in setRep, you compare Low and High with Rep:
    Code:
    if( Rep >= Low && Rep <= High)
    Rep is not assigned a value yet, the validation should be with the parameter r (which is the value entered by the user):
    Code:
    if( r >= Low && r <= High)
    That was it! its all working! thank you for the help! you really helped me through a though spot!
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo