Question

Java- Array Question

Why is the max value 3.5? Explain like you're explaining to a 5 yr old

D Test.java 1 public class Test public static void main (String[] args) 2.9, 3.4, 3 4 double [ ] myList = {1.9, 3.5); for (int i = 0; i <myList. length; i++) System. out.print (myList[i]+); 7 8 9 10 System. out.println (); double total 0; for (int i = 0; i ?myList . Îength; i++) { total += myList [ i ] ; 12 13 14 15 16 17 18 System. out.println (Total is total); double max myList [0]; for (int i = 1; 1 ?myList. Iength; i++) { if (myList [i] > max) max = myList[i]; g Console X terminateds Test (Java Application] C:Program FilesUavalire1.8.0 1211binjavaw.exe (Feb 6, 2017, 8:36:54 .9 2.9 3.4 3.5 I Total is 11.7 Max is 3.5

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

public class Test
{
public static void main(String args[])
{
double[] myList={1.9,2.9,3.4,3.5};
for(int i=0;i<myList.length;i++)
{
System.out.print(myList[i]+" ");
}
System.out.println();
double total=0;
for(int i=0;i<myList.length;i++)
{
total+=myList[i];
}
System.out.println("Total is "+total);


//Taken 1 variable of name max and assign value of first element of given array myList [] to it ie 1.9  

double max=myList[0];   
//Taken one loop which will traversing from one location to another
//i=0 to point first element of myList[] array n it will visit all the locations of given erray until it gets last value
  
for(int i=0;i<myList.length;i++)
{
//Inside loop we have taken if else structure to get max value
//Here we are comparing value of array and the value of max ie already assigned before the loop ie myList[0]=1.9
if(myList[i]>max)
{
//For i=0 myList[0] has value 1.9 and comparing it with max variable value ie 1.9 so it will not satisfy the condition so //it will go for next iteration
//so for i=0 ,max=1.9
//for i=1, myList[1] has value 2.9 and max has 1.9 so it will satisfy the if condtion ie value of myList[1]>max ie 2.9>1.9
//it will come down and assign 2.9 to max and remove previous value ie 1.9 so max got new value ie 2.9
//for i=1, max=2.9
//for i=2, still loop is on so it will go for next value ie i=2 here myList[2]=3.4 so again it will satisfy the condition and assign 3.4 to max and remove/override previous value ie 2.9
//for i=2, max=3.4
//for i=3, now max has value 3.4 and myList has value 3.5 again it will satisfy the condition of if and assign value of myList[2] ie 3.5 to max
//so at this time max has a value 3.5
//again value of i will increase by1 but as it becomes i=4 then it will not satisfy the condition of for so it will come out from the loop
// so at last max is holding value 3.5
max=myList[i];
}
}
//as print the statement you will get the value of max ie 3.5
System.out.println("Max is "+max);
}
}

Add a comment
Know the answer?
Add Answer to:
Java- Array Question Why is the max value 3.5? Explain like you're explaining to a 5...
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
  • please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to...

    please help with the TODO's in bold below! * Java application: CS140_Arrays.java * * Need to be done! * * TODO#1: Change the name and date accordingly * Created by , 11/25/2019 */ public class CS140_Arrays_Done { //TODO#2: Define two private static constants: rows (set to 5) & cols (set to 3) //array for the values private static double[][] values = { { 9, 10, 8 }, { 3.5, 10, 8.5 }, { 10, 8.5, 9 }, { 8.5, 10,...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • 2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority...

    2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans...

    MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans IDE to create the main class called MasterMind.java MasterMind class Method main() should: Call static method System.out.println() and result in displaying “Welcome to MasterMind!” Call static method JOptionPane.showMessageDialog(arg1, arg2) and result in displaying a message dialog displaying “Let’s Play MasterMind!” Instantiate an instance of class Game() constants Create package Constants class Create class Constants Create constants by declaring them as “public static final”: public...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

  • Can you please help me to run this Java program? I have no idea why it...

    Can you please help me to run this Java program? I have no idea why it is not running. import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; @SuppressWarnings({"unchecked", "rawtypes"}) public class SmartPhonePackages extends JFrame { private static final long serialVersionID= 6548829860028144965L; private static final int windowWidth = 400; private static final int windowHeight = 200; private static final double SalesTax = 1.06; private static JPanel panel;...

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc = new Scanner(System.in);         String choice = "y";         while (choice.equalsIgnoreCase("y")) {             // get the input from the user             System.out.println("DATA ENTRY");             double monthlyInvestment = getDoubleWithinRange(sc,                     "Enter monthly investment: ", 0, 1000);             double interestRate = getDoubleWithinRange(sc,                     "Enter yearly interest rate: ", 0, 30);             int years = getIntWithinRange(sc,                     "Enter number of years: ", 0, 100);             System.out.println();            ...

  • Please help me with my JAVA programming Exercise. a. The Talk-A-Lot Cell Phone Company provides phone...

    Please help me with my JAVA programming Exercise. a. The Talk-A-Lot Cell Phone Company provides phone services for its customers. Create an abstract class named PhoneCall that includes a String field for a phone number and a double field for the price of the call. Also include a constructor that requires a phone number parameter and that sets the price to 0.0. Include a set method for the price. Also include three abstract get methods—one that returns the phone number,...

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