0
0
Javaprogramming~15 mins

Array declaration and initialization in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeArray declaration and initialization
📖 Scenario: You are helping a small store keep track of the prices of some fruits they sell. You will create an array to store these prices.
🎯 Goal: Create an array to hold fruit prices, set a size, fill it with values, and then print the prices.
📋 What You'll Learn
Declare an array of type double called fruitPrices
Initialize the array with a size of 4
Assign the prices 1.99, 2.49, 0.99, and 3.49 to the array elements
Print all the prices in the array
💡 Why This Matters
🌍 Real World
Arrays are used to store lists of data like prices, names, or measurements in many software applications.
💼 Career
Understanding arrays is essential for programming jobs because they help manage collections of data efficiently.
Progress0 / 4 steps
1
Declare and initialize the array
Declare an array of type double called fruitPrices and initialize it with size 4.
Java
💡 Need a hint?

Use double[] fruitPrices = new double[4]; to create the array.

2
Assign prices to the array elements
Assign the prices 1.99, 2.49, 0.99, and 3.49 to the array elements fruitPrices[0], fruitPrices[1], fruitPrices[2], and fruitPrices[3] respectively.
Java
💡 Need a hint?

Assign each price to the correct index like fruitPrices[0] = 1.99;

3
Use a for loop to print all prices
Use a for loop with variable i from 0 to 3 to print each price in fruitPrices[i] using System.out.println.
Java
💡 Need a hint?

Use a for loop from 0 to 3 and print each element with System.out.println(fruitPrices[i]);

4
Print all fruit prices
Run the program to print all the fruit prices stored in the fruitPrices array.
Java
💡 Need a hint?

Run the program and check the output matches the prices.