Answer to Question #267670 in Java | JSP | JSF for Ashwari Pillay

Question #267670

Q.1.1 Create a two-dimensional array to contain the three sale items for five different artists. A single array must be used to store the artist names.

ARTIST NAME CD SALES DVD SALES BLU RAY SALES

Ed Sheeran 900 000 800 000 500 000

Pink 700 000 500 000 500 000

Bruno Mars 800 000 100 000 50 000

Foo Fighters 100 000 200 000 200 000

Taylor Swift 300 000 100 000 50 000


Q.1.2 Allow a user to enter in a number, ranging from 1 to 5, which will represent the artist position with regards to the album sales.


Q.1.3 Printout the artist name including the CD, DVD, BLU RAY sales amounts and total sales.


1
Expert's answer
2021-11-17T17:34:20-0500
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    String[] artist = {"Ed Sheeran", "Pink", "Bruno Mars", "Foo Fighters", "Taylor Swift"};
    long[][] sales = new long[5][3];
        sales[0][0] = 900000L;
        sales[0][1] = 800000L;
        sales[0][2] = 500000L;

        sales[1][0] = 700000L;
        sales[1][1] = 500000L;
        sales[1][2] = 500000L;

        sales[2][0] = 800000L;
        sales[2][1] = 100000L;
        sales[2][2] = 50000L;

        sales[3][0] = 100000L;
        sales[3][1] = 200000L;
        sales[3][2] = 200000L;

        sales[4][0] = 300000L;
        sales[4][1] = 100000L;
        sales[4][2] = 50000L;

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the artist position: ");
        String position = sc.nextLine();
        sc.close();
        
        //check input format(only digits)
        if(!position.matches("\\d+")) {
            System.out.println("Wrong format");
        } else if(Integer.parseInt(position) > 5 || Integer.parseInt(position) < 1 ){
            System.out.println("Artist on this position doesn't exist");
        } else {
            int pos = Integer.parseInt(position) - 1;
            System.out.println("Artist: " + artist[pos]);
            long albumSum = sales[pos][0] + sales[pos][1] + sales[pos][2];
            System.out.println("CD: " + sales[pos][0] + ", DVD: " + sales[pos][1] + ", " + "BLU RAY: " + sales[pos][2]);
            System.out.println("Total sales: " + albumSum);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS