Ch 3: 3.1-3.5

Product thing = new Product(......); //create the object thing.reducePrice(percent); //decrease the price by the given percent thing.increasePrice(percent); //increase the price the given percent double price = thing.getPrice(); //get the price String name = thing.getName(); //get the name
Record your participation in Piazza clicker question Lesson4 Q1
Let's write the class together
public class Product
{
private String name;
private double price;
}
Record your participation in Piazza clicker question Lesson4 Q2
What is wrong with this sequence of statements?Product toaster = new Product("Toaster", 39.99);
System.out.println(toaster.reducePrice(10));
Record your participation in Piazza clicker question Lesson4 Q3
public class Circle
{
private double radius;
private double area;
public Circle(double theRadius)
{
radius = theRadius;
area = radius * radius * Math.PI;
}
public double area()
{
return area;
}
public void setRadius(double newRadius)
{
radius = newRadius;
}
}
public class CircleTester
{
public static void main(String[] args)
{
Circle c = new Circle(1);
System.out.println(c.area());
System.out.println("Expected: 3.141592653589793");
c.setRadius(10);
System.out.println(c.area());
System.out.println("Expected: 314.1592653589793");
}
}
Record your participation in Piazza clicker question Lesson4 Q4
What are the local variables of the Product class increasePrice method? Click all that apply.
Record your participation in Piazza clicker question Lesson4 Q5
What are instance variables initialized to by the compiler? Check all that apply.
public void increasePrice(double percent)
{
double amountToIncrease = price * percent / 100;
price = price + amountToIncrease;
}thing.increasePrice(10);
this.price = this.price + amountToIncreaseRecord your participation in Piazza clicker question Lesson4 Q6
What is wrong with this code for the constructor of the Product class?
public Product(String name, double price)
{
price = price;
name = name;
}
Record your participation in Piazza clicker question Lesson4 Q7
Write a Student class. A Student has a name and a total score (sum of all the points earned). It has a method to add a score to the total score, a method to get the total score and a methods to get and set the name.
Record your participation in Piazza clicker question Lesson4 Q8
In the previous activity, why should the total score variable be private? What might happen if it were public?