# Thread: This ap course is so hard :(((

1. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## 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?

Java 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();
} //=======================
itsSecretNumber = 1 + randy.nextInt (10);
} //=======================
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
}```

2. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## 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.

3. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: This ap course is so hard :(((

you could try and use a random number 0-2 (total 3) and
Java 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.
Last edited by Russd772; 01-08-2013 at 09:44 PM.

4. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: This ap course is so hard :(((

Java 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();
} //=======================
itsSecretNumber = 1 + randy.nextInt (10);

} //=======================
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.

5. Member
Join Date
Dec 2012
Location
Des Moines, IA
Posts
35
Rep Power
0

## 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.

6. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## Re: This ap course is so hard :(((

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.

7. ## Re: This ap course is so hard :(((

Please go through the Forum Rules, particularly the third paragraph.

db

8. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## 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
Java 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. */

{    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;
}    //=======================
}```

9. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## 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? ;)

10. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: This ap course is so hard :(((

11. Member
Join Date
Dec 2012
Location
Des Moines, IA
Posts
35
Rep Power
0

## 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.

12. ## Re: This ap course is so hard :(((

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.
Last edited by PhQ; 01-09-2013 at 01:13 AM.

13. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## 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...

14. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## Re: This ap course is so hard :(((

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.
Last edited by Army; 01-09-2013 at 01:29 AM.

15. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## 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?

16. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## 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

17. Member
Join Date
Dec 2012
Posts
42
Rep Power
0

## 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
Last edited by Russd772; 01-09-2013 at 04:26 AM. Reason: added some more

18. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## Re: This ap course is so hard :(((

You missed 110 :)

19. Senior Member
Join Date
Jan 2012
Posts
147
Rep Power
0

## 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.

20. AN21XX
Join Date
Mar 2012
Location
Munich
Posts
297
Rep Power
5

## 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)

Page 1 of 2 12 Last

#### Posting Permissions

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