Purpose : To learn how to create, populate, and access a multidimensional array.
ID: 3710045 • Letter: P
Question
Purpose: To learn how to create, populate, and access a multidimensional array.
Code a 2D array that will store the Oscar categories and the winning film for that category in 2018. Once the array is populated, print the information. Code everything in the main(). Use printf(). Code your program according to the specifications below. There will be embedded for loops and an embedded if...else to determine the prompt. The if...else is in the nested for loop. When the loop-control variable for the column is 0, you'll prompt for the Oscar category; otherwise, you'll prompt for the winning film. Name the programYourLastNameFirstInitialLE63.java.
In the 2-dimensional array (table) below, assume the first row belongs to the first category and the second row belongs to the 2nd category and so forth where the first column is the Oscar category column and the second column is the film title column.
1st Prompt: This prompt will be repeated for each Oscar category where the 9 is 1, 2, or 3.
Enter Oscar category 9:
2nd Prompt: This prompt will be repeated for each film where the X's is the Oscar category.
Enter the film for Xxxxxxxxxxxxx:
Output Specifications: The winning film will be printed for each Oscar category. The print for the header has to be outside of the for loops because it's printed only once. To successfully print the output, you will only loop through the 1st column in the nested for, and you'll use the column index plus 1 to access the film in the 2nd column. This will mean that the test expression for the column index will be 1 less the actual number of columns. Xs represent the Oscar category and film title.
OSCAR WINNERS 2018
Category: Xxxxxxxxxxxx
Film: Xxxxxxxxxxxx
Category: Xxxxxxxxxxxx
Film: Xxxxxxxxxxxx
***END OUTPUT SPECS***
***SAMPLE OUTPUT***
Enter Oscar category 1: Best Picture
Enter the film for Best Picture: The Shape of Water
Enter Oscar category 2: Best Animated Feature Film
Enter the film for Best Animated Feature Film: Coco
Enter Oscar category 3: Best Cinematography
Enter the film for Best Cinematography: Blade Runner 2049
OSCAR WINNERS 2018
Category: Best Picture
Film: The Shape of Water
Category: Best Animated Feature Film
Film: Coco
Category: Best Cinematography
Film: Blade Runner 2049
Best Picture The Shape of Water Best Animated Feature Film Coco Best Cinematography Blade Runner 2049Explanation / Answer
Java code:
public class JoeAlexLE63
{
//main method starts
public static void main(String[] args)
{
String oscarWinningList[][] = new String[3][2];
/* oscarWinningList[0] = new String[] {"Best Picture", "The Shape of Water"};
oscarWinningList[1] = new String[] {"Best Animated Feature Film", "Coco"};
oscarWinningList[2] = new String[] {"Best Cinematography", "Blade Runner 2049"};*/
Scanner sc = new Scanner(System.in);
//for loop to ask the oscar category and file title
for(int i=0;i<oscarWinningList.length;i++)
{
for(int j=0;j<oscarWinningList[i].length;j++)
{
System.out.print("Enter Oscar category "+(i+1)+":");
String category =sc.nextLine();
oscarWinningList[i][j]=category;
System.out.print("Enter the film for "+category+":");
String filmTitle=sc.nextLine();
j=j+1;
oscarWinningList[i][j]=filmTitle;
if(j==0)
continue;
else
break;
}
}
//displaying output
System.out.println(" OSCAR WINNERS 2018 ");
for(int i=0;i<oscarWinningList.length;i++)
{
for(int j=0;j<oscarWinningList[i].length;j++)
{
System.out.printf("Category:%s ",oscarWinningList[i][j]);
j=j+1;
System.out.printf("Film:%s ",oscarWinningList[i][j]);
if(j==0)
continue;
else
break;
}
System.out.println(" ");
}
}
}
Output:
Enter Oscar category 1:Best Picture
Enter the film for Best Picture:The Shape of Water
Enter Oscar category 2:Best Animated Feature Film
Enter the film for Best Animated Feature Film:Coco
Enter Oscar category 3:Best Cinematography
Enter the film for Best Cinematography:Blade Runner 2049
OSCAR WINNERS 2018
Category:Best Picture
Film:The Shape of Water
Category:Best Animated Feature Film
Film:Coco
Category:Best Cinematography
Film:Blade Runner 2049
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.