Look at tracing using the Product class we wrote earlier
public class ProductTester { public static void main(String[] args) { Product thing = new Product("iPad", 450.00); thing.reducePrice(10); thing.increasePrice(10); double myPrice = thing.getPrice(); System.out.println(myPrice); String name = thing.getName(); System.out.println(name); } } |
public class Product { private String name; private double price; public Product(String theName, double thePrice) { name = theName; price = thePrice; } public String getName() { return name; } public double getPrice() { return price; } public void reducePrice(double percent) { double amountToReduce = price * percent /100; price = price - amountToReduce; } public void increasePrice(double percent) { double amountToIncrease = price * percent /100; price = price + amountToIncrease; } } |
Record your participation in Piazza clicker question Lesson6 Q1
This class has a problem. What is it?
Circle
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; } }
CircleTester
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"); } }
So what is the problem?
Record your participation in Piazza clicker question Lesson6 Q2
What are the local variables of the Product class increasePrice method?
public void increasePrice(double percent) { double amountToIncrease = price * percent / 100; price = price + amountToIncrease; }
Click all that apply.
Record your participation in Piazza clicker question Lesson6 Q3
What are local variables initialized to by the compiler? Check all that apply.
Record your participation in Piazza clicker question Lesson6 Q4
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
reference
this
means this objectthis.price = this.price + amountToIncrease;
Record your participation in Piazza clicker question Lesson6 Q5
What is wrong with this code?
private double price; private String name; public Product(String name, double price ) { price = price; name = name; }
Record your participation in Piazza clicker question Lesson6 Q6
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.
Here is a tester for the Student class. Copy it into your project. Does your code pass?