id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

srp4379@lausd.net 2-logic1regular > pizzaSliceParty
prev  |  next  |  chance

public boolean pizzaSliceParty(int slices, boolean isWeekend)

A party is fun on the weekend if the number of pizza slices is 40 or more.
A party is fun on a weekday if the number of pizza slices is between 40 and 60 INCLUSIVE.

Given:
An integer value representing the # of slices, and
A boolean value indicating whether it's the weekend or not.
Return true if the party is fun, false if it's not.

___________________________

Up to now, you have been using a model pattern to solve problems whose method header has a single parameter. Below is how you use that same pattern to solve a method with 2 parameters:

Step 1
Create a return variable of the same type as the method's return type.
For this particular method, the return type is boolean.
Then initialize the variable with a default negative value (in this case false)

public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
}

Step 2
The last line of the method will return that variable.
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  return fun;
}

Step 3
Add an if () statement block.
The body of the block will assign the return variable a different value (in this case true)
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  if () {
    fun = true;
  }
  return fun;
}

Step 4
Use one of the parameters in the condition of the if() statement (between the parentheses).
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  if ( isWeekend ) {
    fun = true;
  }
  return fun;
}

Step 5
Add a second NESTED if() statement to the body of the if() statement,
i.e. INSIDE if (isWeekend) {
Move the return value assignment statement to the body of
this new NESTED if() statement
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  if ( isWeekend ) {
    if () {
      fun = true;
    }
  }
  return fun;
}

Step 6
Use the second parameter appropriately in the condition
of the NESTED if() statement.
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  if ( isWeekend ) {
    if ( 40 <= slices ) {
      fun = true;
    }
  }
  return fun;
}

Step 7
DUPLICATE the entire structure of the NESTED if statements.
Change the condition of the OUTER if() statement
to its OPPOSITE
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  if ( isWeekend ) {
    if ( slices >= 40 ) {
      fun = true;
    }
  }
  if ( !isWeekend ) { // NOT isWeekend
    if ( 40 <= slices ) {
      fun = true;
    }
  }
  return fun;
}

Step 8
Modify the condition of the INNER (nested) if() statement
to be consistent with what the problem requires.
public boolean pizzaSliceParty(int slices, boolean isWeekend) {
  boolean fun = false;
  if ( isWeekend ) {
    if ( 40 <= slices ) {
      fun = true;
    }
  }
  if ( !isWeekend ) { // NOT isWeekend
    if ( 40 <= slices && slices <= 60 ) {
      fun = true;
    }
  }
  return fun;
}


pizzaSliceParty(30, false) → false
pizzaSliceParty(50, false) → true
pizzaSliceParty(70, true) → true

...Save, Compile, Run (ctrl-enter)

public boolean pizzaSliceParty(int slices, boolean isWeekend) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 2

Copyright Nick Parlante 2017 - privacy