Results 1 to 6 of 6
  1. #1
    jch02140 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Question Help with logic problem

    Hi, I have a question involving me to write a program asking user to input the number of small and big bricks, as well as the intended result of the brick wall's length. The program will then return true or false if the wall length is possible

    For example, if user input 3 small bricks and 2 big bricks, the following results input by user should return true. Otherwise, it will return false.

    Results that return true: 1,2,3,5,6,7,8,10,11,12,13

    Here is the code

    Java Code:
    package program1;
    import java.util.*;
    import java.io.*;
    import java.text.*;
    import java.lang.*;
    
    public class Program1 {
    
        public int small, big, goal;
        public boolean result;
        /**
         * @param args the command line arguments
         */
    
        public int getSmall() {
            return this.small;
        }
    
        public void setSmall(int smallBricks) {
            small = smallBricks;
        }
    
        public int getBig() {
            return this.big;
        }
    
        public void setBig(int bigBricks) {
            big = bigBricks;
        }
    
        public int getGoal() {
            return this.goal;
        }
    
        public void setGoal(int goalBricks) {
            goal = goalBricks;
        }
    
        public boolean getResult() {
            return this.result;
        }
    
        public void setResult(boolean resultCheck) {
            result = resultCheck;
        }
    
    
        public static void main(String[] args) {
    
            Program1 program = new Program1();
    
            Scanner keyboard;
            keyboard = new Scanner(System.in);
    
            System.out.println("Please input number of small bricks:");
            program.setSmall(keyboard.nextInt());
            System.out.println("Please input number of big bricks:");
            program.setBig(keyboard.nextInt());
            System.out.println("Please input number of goal bricks:");
            program.setGoal(keyboard.nextInt());
    
            program.makeBricks(program.getSmall(), program.getBig(), program.getGoal());
    
            System.out.println("makeBricks(" + program.getSmall() + ", " + program.getBig() + ", "
                                           + program.getGoal() +") ? " + program.getResult());
           
        }
    
        public void makeBricks(int small, int big, int goal) {
            int newSmall = small * 1;
            int newBig = big * 5;
    
            if ((goal <= newSmall) || (goal % newBig == 0) || (goal % (newSmall + newBig) == 0))  {
                setResult(true);
            }
            else {
                setResult(false);
            }
        }
    }
    The above logic is somewhat wrong or missing something. For example, if I input 3 for small bricks, 5 for big bricks and the results as 23, the program returns false but it should be true...

    I am not sure what is wrong here...
    Last edited by jch02140; 01-01-2011 at 10:10 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jch02140 View Post
    The above logic is somewhat wrong or missing something. For example, if I input 3 for small bricks, 5 for big bricks and the results as 23, the program return false but it should be true...

    I am not sure what is wrong here...
    Shouldn't you also suply the size of the small and big bricks? So you end up with five numbers:

    sb) number of small bricks
    sl) size of a single small brick
    bb) number of big bricks
    bl) size of a single big brick
    tl) goal, i.e. total length.

    So the question becomes, find values for x <= sb and y <= bb such that x*sl+y*bl == tl. Is this interpretation correct?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by jch02140 View Post
    The above logic is somewhat wrong or missing something. For example, if I input 3 for small bricks, 5 for big bricks and the results as 23, the program returns false but it should be true...

    I am not sure what is wrong here...
    when you give 3 , 5 and 23, newSmall = 3 , newBig = 25 and goal = 23.

    goal <= newSmall is false, because 23 cannot be less than 3.

    (goal % newBig == 0) is false as well, because 23 divide by 5 has remainders. So cannot be equal 0.

    (goal % (newSmall + newBig) == 0) is false as well, because 23 divide by 28 cannot have no remainders.

    That's why all of them are false... check your logic again.

  4. #4
    jch02140 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Hi Jos,

    Thank you for your reply. Sorry about that. I left out those info.
    The Question mentioned each small brick is 1 inch long and each big brick is 5 inches long. And the follow method is provided.

    Java Code:
    public boolean makerBricks(int small, int big, int goal)
    Last edited by jch02140; 01-01-2011 at 11:00 AM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jch02140 View Post
    Hi Joe,

    Thank you for your reply. Sorry about that. I left out those info.
    The Question mentioned each small brick is 1 inch long and each big brick is 5 inches long. And the follow method is provided.

    Java Code:
    public boolean makerBricks(int small, int big, int goal)
    Ah, so the length of those bricks is fixed; your problem is easy to solve then:

    Java Code:
    for (int i= nof_small_bricks; i >= 0; i--)
       if ((total-i*1)%5 == 0)
          if ((total-i*1)/5 <= number_of_large_bricks)
             // use i small bricks and (total-i*1)/5 large bricks
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    jch02140 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Inheritance logic problem
    By Gloomy in forum New To Java
    Replies: 3
    Last Post: 08-28-2010, 08:23 AM
  2. Logic not working
    By Prajin in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2010, 07:54 PM
  3. need a logic for this
    By rajivjoshi in forum New To Java
    Replies: 4
    Last Post: 06-12-2010, 02:18 PM
  4. problem with <logic:iterate> tag looping
    By tsaswathy in forum Web Frameworks
    Replies: 0
    Last Post: 09-27-2008, 12:13 PM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •