Question

use java thanks 1.Given the following arrays, write a Java program to generate the output below....

use java thanks

1.Given the following arrays, write a Java program to generate the output below.

   String [] item = {"milo", "water", "coke", "tea", "coffee"};

   double [] price = {3.00, 1.00, 5.00, 2.00, 3.50};

   int [] quantity = {200, 500, 350, 100, 700};

Re-write Question 1 using array list and display the sale of the day report.

Re-write Question 1 using 2-dimensional array and display the sale of the day report.

Write a segment program to replace coke with pepsi with price of RM6.00

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question-1

import java.util.*;
import java.io.*;

public class Sample{

public static void main(String[] args){

String[] item = {"milo","water","coke","tea","coffee"};
double[] price = {3.00,1.00,5.00,2.00,3.50};
int[] quantity = {200,500,350,100,700};

System.out.println(Arrays.toString(item));

System.out.println(Arrays.toString(price));

System.out.println(Arrays.toString(quantity));

}


}


Question-1 Using Array-List

import java.util.*;
import java.io.*;

public class Sample1{

public static void main(String[] args){

String[] item = {"milo","water","coke","tea","coffee"};
double[] price = {3.00,1.00,5.00,2.00,3.50};
int[] quantity = {200,500,350,100,700};

ArrayList<String> itemList = new ArrayList<String>(Arrays.asList(item));
ArrayList<double> priceList = new ArrayList<double>(Arrays.asList(price));
ArrayList<int> QuantityList = new ArrayList<int>(Arrays.asList(quantity));


System.out.println(Arrays.toString(itemList.toArray()));

System.out.println(Arrays.toString(priceList.toArray()));

System.out.println(Arrays.toString(quantityList.toArray()));


for(int i=0; i<itemList.size(); i++){

System.out.println("Sale of " + itemList.get(i) + "is : ");

System.out.print(priceList.get(i)*quantityList.get(i));

}


}


}

Question-1 Using 2-Dimensional Array

import java.util.*;
import java.io.*;

public class Sample2{

public static void main(String[] args){

String[] item = {"milo","water","coke","tea","coffee"};
double[] price = {3.00,1.00,5.00,2.00,3.50};
int[] quantity = {200,500,350,100,700};

double[][] sale = new double[item.length][2];

for(int i=0; i<item.length; i++){

for(int j=0; j<2; j++){

if(j==0)
sale[i][j] = price[i];
else
sale[i][j] = quantity[i];

}
}

System.out.println(Arrays.toString(item));

System.out.println(Arrays.toString(sale));


for(int i=0; i<itemList.size(); i++){

System.out.println("Sale of " + itemList.get(i) + "is : ");

System.out.println(sale[i][0]*sale[i][1]);

}

}


}

Code Snippet to replace coke with pepsi


import java.util.*;
import java.io.*;

public class Sample3{

public static void main(String[] args){

String[] item = {"milo","water","coke","tea","coffee"};
double[] price = {3.00,1.00,5.00,2.00,3.50};
int[] quantity = {200,500,350,100,700};

ArrayList<String> itemList = new ArrayList<String>(Arrays.asList(item));
ArrayList<double> priceList = new ArrayList<double>(Arrays.asList(price));
ArrayList<int> QuantityList = new ArrayList<int>(Arrays.asList(quantity));

int i = itemList.indexOf("coke");

itemList.set(i,"pepsi");
priceList.set(i,6.00);

}


}

Add a comment
Know the answer?
Add Answer to:
use java thanks 1.Given the following arrays, write a Java program to generate the output below....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program that specifies three parallel one dimensional arrays name length, width, and area....

    Write a java program that specifies three parallel one dimensional arrays name length, width, and area. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values in the length and width arrays (thus, area[i] =   length [i]* width [i]) after data has been entered display the following output: (in Java)

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • The goal of this assignment is to practice one dimensional arrays. Objective: The goal of this...

    The goal of this assignment is to practice one dimensional arrays. Objective: The goal of this assignment is to practice one-dimensional arrays. Background: A local FedEx ship center needs support to compute volume of the packages that are delivered on each day Assignment: The shipping center manages deliveries of each day in a single text file. Each text file contains exactly three lines. Each line contains n double numbers representing a dimension of n boxes. The first line represents the...

  • Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a...

    Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a healthy, balanced diet, a grownup needs 2,250 calories a day You will write a program to track calorie intake of a person. Assignment: Calorie intake data from a person is provided in a text file named input.txt. There are arbitrary number of double values on each line, separated by spaces. The numbers represent the number of calories consumed for meals and/or snacks on a...

  • Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs...

    Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs six numbers [Keep the numbers in an array]. The numbers must be between 20 and 200, inclusive. If the number is not between 20 and 200, ask for another input. When a number is entered, display the number only if it is not a duplicate of a number already entered. If it is a duplicate of a number already entered, display a message that...

  • JAVA PLEASE Concepts tested by this program: Variables Input/output .Loops Conditional Statements Methods . Arrays ....

    JAVA PLEASE Concepts tested by this program: Variables Input/output .Loops Conditional Statements Methods . Arrays . Multi-Dimensional Arrays Program 1: Locker Puzzle Problem Description: A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted S1, opens every locker. Then the second student, S2, begins with the second locker, denoted L2, and closes every other locker. Student S3 begins with the third locker and changes...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The...

    c++ show code thanks, only use library <iostream> Question 1 Write the following two functions The first function accepts as input a two-dimensional array of integers. It returns two results: the sum of the elements of the array and the average The second function accepts as input a two-dimensional array of integers and integer value V. It returns true if the value V is found in the array, false otherwise. Write a main program that declares and initializes the following...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers....

    Please complete the lab following the guidelines using JAVA Activity 1 Write a program called AnalyzeNumbers. This program will have array that holds 10 random integers from 1 to 5. Output the 10 random numbers to the screen using an enhanced for loop. This program will also have a method called frequency that takes an integer array as the parameter and it will return an array that holds the frequency of numbers. Index 0 will hold the frequency of 1,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT