I have a project and I am facing a problem I had before again.
We have this Tester file given. Should not edit it.
I want you to focus on this line:
System.out.println(bb) ;
It prints the object right?
import java.util.Arrays ;
/**
Presents some problems to the BillBoard class.
*/
public class BillboardTester
{
public static void main(String[] args)
{
int[] profits = new int[]{1, 2, 3, 1, 6, 10} ;
int k = 2 ;
System.out.println("Profits: " +
Arrays.toString(profits) + " k = " + k) ;
Billboard bb = new Billboard(profits, k) ;
System.out.println("Maximum Profit = " + bb.maximumProfit()) ;
System.out.println(bb) ;
k = 3 ;
profits = new int[]{7, 4, 5, 6, 1, 7, 8, 9, 2, 5} ;
System.out.println("Profits: " +
Arrays.toString(profits) + " k = " + k) ;
bb = new Billboard(profits, k) ;
System.out.println("Maximum Profit = " + bb.maximumProfit()) ;
System.out.println(bb) ;
}
}
Then by printing the object, he expects this result:
Billboards removed (profit): 3(1) 0(1) => profit loss of 2
total value of billboards = 23
remaining maximum profit = 21
I don’t know what method do I have to create in the actual Billboard class, so I can get this printed out. Do you have any suggestions? I want to know the logic behind this, rather than a solution to that particular problem.
Override
toStringmethod.Whenever you print any instance of your class, the
toStringmethod is called. If you don’t override it,Object'sclasstoStringwill be used, which returns a representation havingtype@someNumberform.To print your own representation, just override it, and then your implementation of
toStringwill be invoked.You can change your returned string accordingly. I don’t know what your
kis, but you can also include that in yourreturned string.