These are the write code problems and mixed up code puzzles used by the Unit 3 Toggle Code Practice page. Each problem has two copies that differ only in their label: the Get Help puzzle for the copy whose label ends in -ct-static is always the mixed up code puzzle below it, while the other copyโs Get Help puzzle is personalized by CodeTailor when the course has an API token. The practice page assigns each student one copy of each problem at random.
Write a constructor for the Student class. It takes a name (a String), a grade (an int), and a gpa (a double), in that order, and sets the matching instance variables. Then write a toString method that returns the studentโs information in the form name (grade grade): gpa, for example Ana (grade 10): 3.8.
Write a constructor for the Student class. It takes a name (a String), a grade (an int), and a gpa (a double), in that order, and sets the matching instance variables. Then write a toString method that returns the studentโs information in the form name (grade grade): gpa, for example Ana (grade 10): 3.8.
Write a constructor for the Student class. It takes a name (a String), a grade (an int), and a gpa (a double), in that order, and sets the matching instance variables. Then write a toString method that returns the studentโs information in the form name (grade grade): gpa, for example Ana (grade 10): 3.8. The blocks below are mixed up and include extra blocks that are not needed. Drag the needed blocks from the left and put them in order on the right, with the right indentation. Click Check to check your solution.
Write a getPrice method for the Ticket class. It returns 5 if age is under 12, and 10 otherwise. The blocks below are mixed up and include extra blocks that are not needed. Drag the needed blocks from the left and put them in order on the right, with the right indentation. Click Check to check your solution.
public class Ticket
{
---
private int age;
---
public Ticket(int age)
{
---
this.age = age;
---
} // end constructor
---
public int getPrice()
{
---
public void getPrice()
{ #paired
---
if (age < 12)
{
---
if (age <= 12)
{ #paired
---
return 5;
---
} // end if
---
return 10;
---
} // end getPrice
---
public static void main(String[] args)
{
---
System.out.println(new Ticket(8).getPrice());
System.out.println(new Ticket(12).getPrice());
System.out.println(new Ticket(40).getPrice());
---
} // end main
---
} // end class
Write a withdraw method for the BankAccount class. It takes an intamount. If the account has at least that much money, subtract amount from balance. Otherwise, leave balance unchanged.
Write a withdraw method for the BankAccount class. It takes an intamount. If the account has at least that much money, subtract amount from balance. Otherwise, leave balance unchanged.
Write a withdraw method for the BankAccount class. It takes an intamount. If the account has at least that much money, subtract amount from balance. Otherwise, leave balance unchanged. The blocks below are mixed up and include extra blocks that are not needed. Drag the needed blocks from the left and put them in order on the right, with the right indentation. Click Check to check your solution.
Write two methods for the Rectangle class. getArea returns width times height. isLargerThan takes another Rectangle and returns true if this rectangleโs area is larger than the other rectangleโs area.
Write two methods for the Rectangle class. getArea returns width times height. isLargerThan takes another Rectangle and returns true if this rectangleโs area is larger than the other rectangleโs area.
Write two methods for the Rectangle class. getArea returns width times height. isLargerThan takes another Rectangle and returns true if this rectangleโs area is larger than the other rectangleโs area. The blocks below are mixed up and include extra blocks that are not needed. Drag the needed blocks from the left and put them in order on the right, with the right indentation. Click Check to check your solution.
public class Rectangle
{
---
private int width;
private int height;
---
public Rectangle(int width, int height)
{
---
this.width = width;
this.height = height;
---
} // end constructor
---
public int getArea()
{
---
return width * height;
---
} // end getArea
---
public boolean isLargerThan(Rectangle other)
{
---
public boolean isLargerThan()
{ #paired
---
return this.getArea() > other.getArea();
---
return this.getArea() > other; #paired
---
} // end isLargerThan
---
public static void main(String[] args)
{
---
Rectangle a = new Rectangle(4, 5);
Rectangle b = new Rectangle(3, 6);
System.out.println(a.getArea());
System.out.println(a.isLargerThan(b));
System.out.println(b.isLargerThan(a));
---
} // end main
---
} // end class
Every PizzaOrder shares the same price per slice, stored in the static variable pricePerSlice. Write a static method setPricePerSlice that takes a doubleprice and changes pricePerSlice to it. Then write a getTotal method that returns the orderโs total cost: slices times pricePerSlice.
Every PizzaOrder shares the same price per slice, stored in the static variable pricePerSlice. Write a static method setPricePerSlice that takes a doubleprice and changes pricePerSlice to it. Then write a getTotal method that returns the orderโs total cost: slices times pricePerSlice.
Every PizzaOrder shares the same price per slice, stored in the static variable pricePerSlice. Write a static method setPricePerSlice that takes a doubleprice and changes pricePerSlice to it. Then write a getTotal method that returns the orderโs total cost: slices times pricePerSlice. The blocks below are mixed up and include extra blocks that are not needed. Drag the needed blocks from the left and put them in order on the right, with the right indentation. Click Check to check your solution.