[an error occurred while processing this directive] [an error occurred while processing this directive]
Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Goals for today:
You earn 1 point for each lab that you attend/show up on time for. Receiving credit for CSE 190 requires attending 8-10 labs. Please check that all your attendance records are correct on Canvas!
If you believe there is a problem with one of your scores, please email Fadel Shtiui at pludwig@cs.washington.edu.
Arrays.toString()
, use [] around the array.)S.o.p()/S.o.pln()
for System.out.print() / System.out.println()
, respectively.
Reference mysteries test your understanding of reference semantics and value semantics. Review the difference in lab 8, slide 12!
On the following slide, write the output resulting from the execution of each corresponding line of code:
Note: Remember that printing arrays using Arrays.toString()
prints using [], not {}.
public static void main(String[] args) { int y = 1; int x = 3; int[] a = new int[4]; mystery(a, y, x); // 2 3 [0, 0, 17, 0]^0-9,+ System.out.println(x + " " + y + " " + Arrays.toString(a)); // 3 1 [0, 0, 17, 0]^0-9,+ x = y - 1; mystery(a, y, x); // 1 0 [17, 0, 17, 0]^0-9,+ System.out.println(x + " " + y + " " + Arrays.toString(a)); // 0 1 [17, 0, 17, 0]^0-9,+ } public static void mystery(int[] a, int x, int y) { if (x < y) { x++; a[x] = 17; } else { a[y] = 17; } System.out.println(x + " " + y + " " + Arrays.toString(a)); }
Write a method name groceries
that accepts as its parameter a Scanner
holding an input file. The data in the file represents grocery items purchased along with their price and their discount category. Your method should compute and return a real number representing the total cost of the grocery items.
Each item is represented by three tokens: (1) the name of the item (a single word), (2) its discount category ("red" (10% off full price), "blue" (25% off full price), or "none" (no discount)), and (3) its full price. The casing of the discount category is not guaranteed (for example, we could see "red", "RED", "rED", "rEd", or any other casing of the word "red").
Example input:avocado RED 1 blueberries none 5 milk blue 2.00 cream red 1.00 cereal None 1.29This should return 9.59.
If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.
You can view an exercise, type a solution, and submit it to see if you have solved it correctly.
Choose some problems from the book and try to solve them!