{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

UppChapter7F - Chapter 7 Arrays More Lab Exercises Topics...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 7: Arrays & More Lab Exercises Topics Lab Exercises One-Dimensional Arrays Tracking Sales Working with arrays Cryptography Arrays of Objects A Shopping Cart ( Friviligt uppgift för IT –forensik) Mouse Events Drawing Circles with Mouse Clicks Moving Circles with the Mouse
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Tracking Sales File Sales.java contains a Java program that prompts for and reads in the sales for each of 5 salespeople in a company. It then prints out the id and amount of sales for each salesperson and the total sales. Study the code, then compile and run the program to see how it works. Now modify the program as follows: 1. Compute and print the average sale. (You can compute this directly from the total; no loop is necessary.) 2. Find and print the maximum sale. Print both the id of the salesperson with the max sale and the amount of the sale, e.g., "Salesperson 3 had the highest sale with $4500." Note that you don't need another loop for this; you can do it in the same loop where the values are read and the sum is computed. 3. Do the same for the minimum sale. 4. After the list, sum, average, max and min have been printed, ask the user to enter a value. Then print the id of each salesperson who exceeded that amount, and the amount of their sales. Also print the total number of salespeople whose sales exceeded the value entered. 5. The salespeople are objecting to having an id of 0—no one wants that designation. Modify your program so that the ids run from 1–5 instead of 0–4. Do not modify the array —just make the information for salesperson 1 reside in array location 0, and so on. 6. Instead of always reading in 5 sales amounts, at the beginning ask the user for the number of sales people and then create an array that is just the right size. The program can then proceed as before. // **************************************************************** // Sales.java // // Reads in and stores sales for each of 5 salespeople. Displays // sales entered by salesperson id and total sales for all salespeople. // // **************************************************************** import java.util.Scanner; public class Sales { public static void main(String[] args) { final int SALESPEOPLE = 5; int[] sales = new int[SALESPEOPLE]; int sum; Scanner scan = new Scanner(System.in); for (int i=0; i<sales.length; i++) { System.out.print("Enter sales for salesperson " + i + ": "); sales[i] = scan.nextInt(); } System.out.println("\nSalesperson Sales"); System.out.println("--------------------"); sum = 0; for (int i=0; i<sales.length; i++) { System.out.println(" " + i + " " + sales[i]); sum += sales[i]; }
Image of page 2
System.out.println("\nTotal sales: " + sum); } }
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Working with arrays 1. What will be printed by the following code segment? int arr[] = { 24,0,19,21,6,-5,10,16}; for(int i=0;i<arr.lenght;i++) System.out.println(arr[i]); 2. What will be printed by the following code segment? int arr[] = { 24,0,19,21,6,-5,10,16}; for(int k =0;i<arr.lenght; k=k+2) System.out.println(arr[k]); 3. Compile and run the following class. Understand how it works and implement the c, d, and e functionalities in the text below.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern