# This ap course is so hard :(((

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-08-2013, 09:18 PM
Army
This ap course is so hard :(((
Exercise 4.28* Revise the GuessNumber game to have the program lie one-third of the time that the guess is too high; it says it is too low. But it never lies about the guess being too low, nor does it lie twice in a row.

How do I even think of doing that? Wtf?

Code:

```import javax.swing.JOptionPane; public class GuessNumber extends BasicGame {     private java.util.Random randy;     private int itsSecretNumber;     private int itsUsersNumber;     public GuessNumber(){         super();         randy = new java.util.Random();     } //=======================     public void askUsersFirstChoice(){         itsSecretNumber = 1 + randy.nextInt (10);         askUsersNextChoice();     } //=======================     public void askUsersNextChoice(){         String s = JOptionPane.showInputDialog         ("Guess my number from 1 to 100:");             if (s != null && ! s.equals ("")) {             itsUsersNumber = Integer.parseInt (s);             }else{             itsUsersNumber = -1; // just to have a value there         }               } //======================         public boolean shouldContinue(){             return itsUsersNumber != itsSecretNumber;         } //======================         //Exercise 4.28         public void showUpdatedStatus(){             int result = itsUsersNumber - itsSecretNumber;             if(Math.abs(result) == 2){                 JOptionPane.showMessageDialog (null, "You're hot");             }             if(Math.abs(result) == 1){                 JOptionPane.showMessageDialog (null, "You're hot");             }             if(Math.abs(result) == 3){                 JOptionPane.showMessageDialog (null, "You're warm");             }             if(Math.abs(result) == 4){                 JOptionPane.showMessageDialog (null, "You're warm");             }             if(Math.abs(result) == 5){                 JOptionPane.showMessageDialog (null, "You're warm");             }             if (itsUsersNumber > itsSecretNumber)             JOptionPane.showMessageDialog (null, "Too high");             else             JOptionPane.showMessageDialog (null, "Too low");         } //=======================         // inherited from BasicGame:         // playManyGames         // playOneGame         // showFinalStatus }```
• 01-08-2013, 09:39 PM
Sierra
Re: This ap course is so hard :(((
Implement one thing at a time, step by step. One third is a probability, so you need to generate a random number and use a conditional to determine if it lies or not...
I suggest you implement methods for "too high" and "too low", so you do not have to duplicate code.
• 01-08-2013, 09:42 PM
Russd772
Re: This ap course is so hard :(((
you could try and use a random number 0-2 (total 3) and
Code:

```if(randNum == 1 && guessedNumber < secretNumber){ System.out.println("Number is too High"); } if(randNum == 1 && guessedNumber > secretNumber){ System.out.println("Number is too Low"); }```
or something along those lines
this would give a lie 33% of the time but would lie depending on the users guess, if you just want to say high no matter what then technically its not a lie 33% of the time if the user guesses too low, but you can just omit the greater than and less than expressions.
• 01-08-2013, 09:48 PM
Army
Re: This ap course is so hard :(((
Code:

```import javax.swing.JOptionPane; public class GuessNumber extends BasicGame {     private java.util.Random randy;     private int itsSecretNumber;     private int itsUsersNumber;     private int itsMark;     public GuessNumber(){         super();         randy = new java.util.Random();     } //=======================     public void askUsersFirstChoice(){         itsSecretNumber = 1 + randy.nextInt (10);         askUsersNextChoice();           } //=======================     public void askUsersNextChoice(){         String s = JOptionPane.showInputDialog         ("Guess my number from 1 to 100:");             if (s != null && ! s.equals ("")) {             itsUsersNumber = Integer.parseInt (s);             }else{             itsUsersNumber = -1; // just to have a value there         }       itsMark++;         } //======================         public boolean shouldContinue(){             return itsUsersNumber != itsSecretNumber;                     } //======================         //Exercise 4.28         public void showUpdatedStatus(){             while(itsMark == itsMark + 2){                 if (itsUsersNumber > itsSecretNumber){                     JOptionPane.showMessageDialog (null, "Too high");                 }             }             int result = itsUsersNumber - itsSecretNumber;             if(Math.abs(result) == 2){                 JOptionPane.showMessageDialog (null, "You're hot");             }             if(Math.abs(result) == 1){                 JOptionPane.showMessageDialog (null, "You're hot");             }             if(Math.abs(result) == 3){                 JOptionPane.showMessageDialog (null, "You're warm");             }             if(Math.abs(result) == 4){                 JOptionPane.showMessageDialog (null, "You're warm");             }             if(Math.abs(result) == 5){                 JOptionPane.showMessageDialog (null, "You're warm");             }             if (itsUsersNumber > itsSecretNumber)             JOptionPane.showMessageDialog (null, "Too high");             else             JOptionPane.showMessageDialog (null, "Too low");         } //=======================         // inherited from BasicGame:         // playManyGames         // playOneGame         // showFinalStatus }```
I did this but my tiny mind doesn't perceive some of the stuff I did.
• 01-08-2013, 10:05 PM
foulkelore
Re: This ap course is so hard :(((
I agree with Sierra. Don't think of this as one large problem, but a series of small problems. Get one thing to work and then move on to another.
• 01-08-2013, 10:10 PM
Russd772
Re: This ap course is so hard :(((
Quote:

Originally Posted by foulkelore
I agree with Sierra. Don't think of this as one large problem, but a series of small problems. Get one thing to work and then move on to another.

Yeah def want to break it down and look at chunks of code, then once you learn what they all do the program as a whole will fal into perspective.
• 01-08-2013, 10:14 PM
DarrylBurke
Re: This ap course is so hard :(((
Please go through the Forum Rules, particularly the third paragraph.

db
• 01-08-2013, 11:00 PM
Army
Re: This ap course is so hard :(((
Sorry if I'm getting annoying but, what does this mean? Exercise 4.33* Write out the full Time constructor with the heading public Time(int totalMinutes). Be sure to allow for negative inputs.

The one thing that is confusing me is the totalMinutes part. Are they telling me to forgot the hours of the time and not use it...?

Here's the Time class
Code:

```public class Time extends Object {     private int itsHour;    // always in the range 0 to 23     private int itsMin;    // always in the range 0 to 59     /** Create an object for the given hour and minute.  If min       *  is negative, adjust the values to make 0 <= min < 60. */     public Time (int hour, int min)            // constructor     {    super();           itsHour = hour;           for (itsMin = min;  itsMin < 0;  itsMin = itsMin + 60)               itsHour--;     }    //=======================     /** Return the time expressed in military time. */     public String toString()     {    if (itsHour < 10)               return ("0" + itsHour) + itsMin;           else               return ("" + itsHour) + itsMin;     }    //=======================     /** Return the result of adding this Time to that Time. */     public Time add (Time that)     {    Time valueToReturn = new Time (this.itsHour + that.itsHour,                                         this.itsMin + that.itsMin);           if (valueToReturn.itsMin >= 60)           {    valueToReturn.itsMin = valueToReturn.itsMin - 60;               valueToReturn.itsHour++;           }           valueToReturn.itsHour = valueToReturn.itsHour % 24;           return valueToReturn;     }    //======================= }```
• 01-08-2013, 11:25 PM
Sierra
Re: This ap course is so hard :(((
I think you would need to calculate those minutes into hours and minutes:
if totalMinutes is 72 it would be one hour and 12 minutes, right? ;)
• 01-08-2013, 11:35 PM
Army
Re: This ap course is so hard :(((
• 01-08-2013, 11:59 PM
foulkelore
Re: This ap course is so hard :(((
I'm not sure programming is a good career choice for you. have you tried going through the tutorals? They are really helpful.
• 01-09-2013, 12:09 AM
PhQ
Re: This ap course is so hard :(((
Quote:

Originally Posted by foulkelore
I'm not sure programming is a good career choice for you.

I'm sure it's impossible for one to learn programming in a couple lectures.
I ask hundreds of questions on this forum and this doesn't mean that programming is not a good career choice for me. Programming might be hard for someone who is new to it.
• 01-09-2013, 12:11 AM
Sierra
Re: This ap course is so hard :(((
True, I never saw someone who came from any course as a professional... it needs time and effort. For some more, for others less... :(happy):
• 01-09-2013, 01:16 AM
Army
Re: This ap course is so hard :(((
Quote:

Originally Posted by foulkelore
I'm not sure programming is a good career choice for you. have you tried going through the tutorals? They are really helpful.

Hmm, that was kinda mean. Sorry if I don't meet your programming standards. Arnold Schwarzenegger or Ronnie Coleman didn't start off squatting 500 lbs on the first day.

And also it's pretty hard to ask an online book questions.
• 01-09-2013, 03:27 AM
Army
Re: This ap course is so hard :(((
Exercise 4.37* There are eight possible assignments of true and false to the boolean variables b, c, and d. Which assignments give !b && (c || !d) a different value from !(b || !c && d).

Does anyone under stand this? How can you have an answer if there is nothing to compare it to be true or false with?
• 01-09-2013, 04:16 AM
Russd772
Re: This ap course is so hard :(((
have you ever studied boolean algebra? or logic? look up "logical OR" and "Logical AND" and that should help alot for you. just consider the true and false 1's and zeroes for that purpose its all the same, "1" being TRUE and "0" being FALSE
• 01-09-2013, 04:18 AM
Russd772
Re: This ap course is so hard :(((
actually include your search to include "inverters", "NAND" and "NOR" also, its not really complicated but it takes a bit of explaining

basically && is the AND and || is the OR, so AND means that ALL values must be true in order for the statement to be true, and OR means that only one value must be true in order for the statement to be true.

when you see a ! for examplye !b means that b has to evaluate to False to be considered TRUE.. im probably confusing the crap out of you, google is probably much better at explaining this.

probably the easiest way to look at all possible outcomes would be to look at it like this

a | b | c
0 0 0 = ?
0 0 1 = ?
0 1 0 = ?
0 1 1 = ?
1 0 0 = ?
1 0 1 = ?
1 1 1 = ?

these are all of the possible variations with 3 variables, 1 is true, 0 is false, you would do the same with the second statement then compare your answers to get your final answer
• 01-09-2013, 10:57 AM
Sierra
Re: This ap course is so hard :(((
You missed 110 :)
• 01-09-2013, 05:16 PM
Army
Re: This ap course is so hard :(((
Before I submitted the question I did write that out but I still was confused. All I'm confused about is what the && and the || effects.
• 01-09-2013, 05:22 PM
Sierra
Re: This ap course is so hard :(((
It is not of much use to tell us that you are confused. You need to tell us what you want from us... explanation? Does something not work as expected?

Take a look here: Summary of Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last