To learn how to create, populate, and access arrays using value-receiving and value-returning methods.

Lab Exercise 6.2 Instructions:  Code a program that prompts the user for the size of the array.  Using the size, the program creates several arrays that will store information about your favorite sports and teams.  There will be one array for the sport, another for the team, and the last one for the year of your team's last championship.  The values in each array at the same location are for a given sport.  Once the arrays are populated, print the information from each array.  The size and input will be the only global variables.  Use printf().  Code your program according to the specifications below.  Use the looping structure generally associated with array processing.  Name the programYourLastNameFirstInitialLE62.java.  The methods will be called from the main().

To learn how to create, populate, and access arrays using value-receiving and value-returning methods.


1st Input Prompt:  Code this in a method named arraySize().  

How many sports are you interested in? 

2nd Input Prompt:  In a method named setFavoriteSports declare the sports array and populate it.  This method will return the sports array to the calling statement.  The 9 in the prompt below will print 1 for the first sport, then 2 for the 2nd one and so forth.  Use the loop-control variable as the value for 9. 

Enter sport #9: 

3rd Input Prompt:  In a method named setFavoriteTeams declare the teams array and populate it.  The Xs in the prompt is the sport.  ThesetFavoriteTeams method will return the teams array to the calling statement as well as receive the sports array from the calling statement.  

Enter your favorite Xxxxxxxxxx team: 

4th Input Prompt:  In a method named setLastChampionship declare the year array and populate it.  The Xs in the prompt is the team name.  The setLastChampionship method will return the year array to the calling statement as well as receive the team array from the calling statement.  This method will be called as an argument in the call to printSportsTeams(). 

Enter the year of the Xxxxxxxxxxxx' last championship: 

Output Specifications:  Where the Xs represent the sport and team and the Zs the year.  Code the output print statement in a method named printSportsTeams() that accepts the sports, teams and year arrays.  In the printSportsTeams method use a for loop to print the information from each array.  The print for the header has to be outside of the for loop because it's printed only once. 


MY SPORTS TEAMS

Sport:  Xxxxxxxxxxxx
Team:  Xxxxxxxxxxxx
Last Championship:  ZZZ9

Sport:  Xxxxxxxxxxxx
Team:  Xxxxxxxxxxxx
Last Championship:  ZZZ9

***END OUTPUT SPECS***

Sample Output:  Follow the spacing and line advances indicated in this output.


The answer

Description:

This lab exercise will help you learn how to create, populate, and access arrays using value-receiving and value-returning methods in Java. You will write a program that prompts the user for the number of sports they are interested in, then creates and populates three parallel arrays: one for sports, one for teams, and one for the year of each team's last championship. The program will then display the collected data in a formatted output.


Explanation:

The program consists of the following steps:

  1. Ask the user how many sports they are interested in using a method that returns the array size.

  2. Create and fill an array of sports using a value-returning method.

  3. Create and fill an array of favorite teams, receiving the sports array as a parameter.

  4. Create and fill an array for the last championship year for each team, receiving the teams array as a parameter.

  5. Display all the information using a method that receives all three arrays as parameters and prints the data in the specified format.

All user input is handled with Scanner, and output is formatted with printf(). The only global variables allowed are the size and the Scanner object for input.


Solution (Java code):

java

import java.util.Scanner; public class YourLastNameFirstInitialLE62 { // Global variables for size and input static int size; static Scanner input = new Scanner(System.in); public static void main(String[] args) { size = arraySize(); String[] sports = setFavoriteSports(size); String[] teams = setFavoriteTeams(sports); int[] years = setLastChampionship(teams); printSportsTeams(sports, teams, years); } // Prompt user for array size public static int arraySize() { System.out.print("How many sports are you interested in? "); return input.nextInt(); } // Create and populate sports array public static String[] setFavoriteSports(int s) { input.nextLine(); // Consume leftover newline String[] sports = new String[s]; for (int i = 0; i < s; i++) { System.out.printf("Enter sport #%d: ", i + 1); sports[i] = input.nextLine(); } return sports; } // Create and populate teams array public static String[] setFavoriteTeams(String[] sports) { String[] teams = new String[sports.length]; for (int i = 0; i < sports.length; i++) { System.out.printf("Enter your favorite %s team: ", sports[i]); teams[i] = input.nextLine(); } return teams; } // Create and populate years array public static int[] setLastChampionship(String[] teams) { int[] years = new int[teams.length]; for (int i = 0; i < teams.length; i++) { System.out.printf("Enter the year of the %s' last championship: ", teams[i]); years[i] = input.nextInt(); } return years; } // Print all arrays in formatted output public static void printSportsTeams(String[] sports, String[] teams, int[] years) { System.out.println("\nMY SPORTS TEAMS\n"); for (int i = 0; i < sports.length; i++) { System.out.printf("Sport: %s\n", sports[i]); System.out.printf("Team: %s\n", teams[i]); System.out.printf("Last Championship: %d\n\n", years[i]); } } }

Summary of Steps:

  • User enters how many sports they are interested in.

  • Arrays are created and populated with favorite sports, teams, and championship years.

  • All data is displayed in the required format.



📩 Need a similar solution? Email me: adel455@hotmail.com









Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming