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

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0

    Programming difficulties


    I have to try and make a program that when a user enters a digit it will say "A #(spelled out) has been entered, and it is (even/odd)." and that that sentence is repeated that number of times. for example
    A three was entered, and it is odd
    A three was enetered, and it is odd
    A three was enetered, and it is odd

    but i have to build it around the following code without editing it.
    "
    package Lab;

    import java.util.Scanner;

    public class Lab
    {
    public static void main(String[] args)
    {
    Box box = new Box(1,10);
    String strUserInst;
    Scanner scan = new Scanner( System.in );


    do {
    strUserInst = "Enter the number of repetitions (between " + box.getHigh() + " and " + box.getLow() + " ): ";
    System.out.print( strUserInst );

    } while( box.setRep( scan.nextInt() ) );

    System.out.println( box );

    }
    }
    "

    The problem is I cant quite understand what is happening in the given code or how to work off of it.
    Any ideas on what the given code is doing and how I might go about working around it?
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    Please use code tags when posting code as this will keep the code format/indention and thereby make the code easier to read.

    For you question, you will have to create a Class called Box, which should contain following functions:
    - class constructor with parameter for the maximum and minimum boundary of repetition
    - getHigh() and getLow() to return respectively the maximum and minimum number of (line) repetitions.
    - setRep(int) to set the number of repetition, this should validate the boundary and return either false or true.
    - toString() to output the lines; as it is called inside a println() function you will have to create multiple lines by using the "character control" '\n'

    IMO, the code you have been giving could be better.
    If code is to be used for those with basic or less knowledge, it try to do to much in the while loop.
    For a more advanced topic, it lack exception on the scanner part (try enter something that is not an integer!?)
    Last edited by MrFujin; May 25th, 2013 at 09:00 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    Please use code tags when posting code as this will keep the code format/indention and thereby make the code easier to read.

    For you question, you will have to create a Class called Box, which should contain following functions:
    - class constructor with parameter for the maximum and minimum boundary of repetition
    - getHigh() and getLow() to return respectively the maximum and minimum number of (line) repetitions.
    - setRep(int) to set the number of repetition, this should validate the boundary and return either false or true.
    - toString() to output the lines; as it is called inside a println() function you will have to create multiple lines by using the "character control" '\n'

    IMO, the code you have been giving could be better.
    If code is to be used for those with basic or less knowledge, it try to do to much in the while loop.
    For a more advanced topic, it lack exception on the scanner part (try enter something that is not an integer!?)

    Thank you for the response, however I am not sure how to go about this. How would you write the lines for that into this given code?
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    Regarding the multiple lines, the syntax would some thing like this:
    Code:
    String multipleLinesText = "Line 1\nLine 2";
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    Regarding the multiple lines, the syntax would some thing like this:
    Code:
    String multipleLinesText = "Line 1\nLine 2";
    I more confused by the following

    - class constructor with parameter for the maximum and minimum boundary of repetition
    - getHigh() and getLow() to return respectively the maximum and minimum number of (line) repetitions.

    Ive never used these parameters before and am not really sure how they work exactly. I need to be able to link it to a scanner input. Im just not sure how to tie them together.
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    My last reply was for this part:
    - toString() to output the lines; as it is called inside a println() function you will have to create multiple lines by using the "character control" '\n'
    Do you know how to create your own class?
    Do you know what a constructor is? and how its work?

    If you don't know the above, I will suggest you look for a tutorial that explain these things.

    Below is an example of a class with a constructor and a function:
    Code:
    class myClass
    {
    	int myClassNumber
    
    	// Constructor
    	myClas(int passedNumber)
    	{
    		myClassNumber = passedNumber;
    	}
    	
    	public int getNumber()
    	{
    		return myClassNumber;
    	}
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    this is what ive tried thus far but am running into a compiler error with the given code for set rep requiring boolean but when I change the type it still wont accept it. Im not sure how to go about fixing it. I just want to get it to respond and print out the prompt so I can work from there on the rest. heres what i have

    PHP Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package box.application;

    /**
     *
     * @author Alex Kramer
     */
    public class Box {
        
        private 
    int HighLow ;
        private 
    int Rep;
        
        private static 
    int counter 0;
        
        public 
    Box(){
         
    High 10 ;
         
    Low ;
         
    counter++;
        }
        public 
    Box (int h,int L) {
            
    High h;
            
    Low L;
        }
        
        public 
    void setHigh(int h){
            
    High h;
        }
        public 
    void setLow(int L){
            
    Low L;
        }
         public 
    void setRep(int r){
            
    Rep r;
        }
         
    /**
         *
         * @return
         */
        
    public int getHigh() {

            return 
    High;
        }
        public 
    int getLow() {

            return 
    Low;
        }
       
        public 
    int getRep (){
            return 
    Rep;
        }

  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    You are correct that setRep should return a boolean value (true/false).
    You also want to check/validate if the parameter is between high and low.
    Take notice of how it is used in the while loop; it should return true if the validation is not correct. (Returning false will stop the loop and thereby no longer ask user for input).
    Last edited by MrFujin; May 26th, 2013 at 02:23 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    How would you fix that? It will not work with the fixes I've tried. I'm using netbeans and it won't compile right.
  18. #10
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    Please post the code showing what (fixes) you have tried.
    If you get any error message, please post those as well (preferable copy/paste) .
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    how would you check if the paremeter is between high and low? Also how do I need to write the lines in the code I have to make setRep return a boolean? Ive tried this

    [PHPNET=""]private boolean Rep;

    public void setRep (boolean r) {
    Rep = r ; }

    public boolean getRep () {
    return Rep ; }[/PHPNET]

    But when I do that in the insert program I get this message

    "method setRep in class Box cannot be applied to given types;
    required: boolean
    found: int
    reason: actual argument int cannot be converted to boolean by method invocation conversion"

    this is where the error occurs Code:
    } while(  box.setRep( scan.nextInt() ) );
  22. #12
  23. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    You can read more about the control statement If-Then-else.

    What you want to validate:
    Code:
    if(r > Low && r < High)
    {
     rep = r;
      return false;
    }
    else 
    {
     return true;
    }
    You don't need the getRep() function to complete this assignment
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by MrFujin
    You can read more about the control statement If-Then-else.

    What you want to validate:
    Code:
    if(r > Low && r < High)
    {
     rep = r;
      return false;
    }
    else 
    {
     return true;
    }
    You don't need the getRep() function to complete this assignment

    I tried this and it wont compile right. I edited it as best I could to make it work in netbeans but it continues to say that the return statements are unnecessary. How can I fix this?

    PHP Code:
        int r;
        { if (
    >= Low ){ 
            if (
    <= High){
                
    Rep r;
                return 
    false; }
            else {return 
    true;} } 
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    ok so I got everything working, however, when I run the code block I was given it runs things out of order. How can I fix this?

    This is my box class
    PHP Code:
    package box.application;

    import java.util.Scanner;
    /**
     *
     * @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 r
         */
        
    public boolean setRep(int r){
            
    Rep r;
            return 
    false;
        }
    Scanner scanner = new Scanner(System.in);     

    int r scanner.nextInt();
    /**
         *
         * @return
         */
        
    public int getHigh() {

            return 
    High;
        }
        public 
    int getLow() {

            return 
    Low;
        }
        
        { if (
    >= Low ){ 
            if (
    <= High){
                
    Rep r;
     }
            else {
    } }
            
        }
        
    String EvenOdd;
        {if(
    r%== 0) {EvenOdd "Even";
        }
        else {
    EvenOdd "Odd";}
        }
        {   
    int number r;
            
    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 (
    r){
                
            
    System.out.println("A " numberString " was enetered, and it is " EvenOdd);
            
    n++;
            }
    }


    This is the class that was provided that runs the box class.
    PHP Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package box.application;

    import java.util.Scanner;

    public class 
    Lab1insert
    {
        public static 
    void main(String[] args
        {
                
    Box          box = new Box(1,10);
                
    String       strUserInst;
                
    Scanner    scan = new ScannerSystem.in );
                
                
                do {  
                        
    strUserInst "Enter the number of repetitions (between " box.getHigh() + " and " box.getLow() + " ): ";
                        
    System.out.print( strUserInst );
                        
                } while(  
    box.setRepscan.nextInt() ) );

                
    System.out.printlnbox );

        }

    When I run the provided class Lab1insert it runs the box class first and then prints out its line. I need to switch that order and it is done.
  28. #15
  29. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,730
    Rep Power
    1959
    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.
    Last edited by MrFujin; May 27th, 2013 at 03:21 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo