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
  • 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 cannot seem to find a single answer to this question. The output of this code...

    I cannot seem to find a single answer to this question. The output of this code is You entered: 236.0 89.5 142.0 166.3 93.0 with a trailing space at the end of the number 93.0. These inputs are changed and always have a trailing space on the last number. Now, I know it's because I have System.out.print(m[i] + " "); with the " " creating the space in between each number. How do I write it so each number has...

  • 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;...

  • 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();            ...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • This is the question: These are in Java format. Comments are required on these two Classes...

    This is the question: These are in Java format. Comments are required on these two Classes (Student.java and StudentDemo.java) all over the coding: Provide proper comments all over the codings. --------------------------------------------------------------------------------------------------------------- import java.io.*; import java.util.*; class Student {    private String name;    private double gradePointAverage;    public Student(String n , double a){    name = n;    gradePointAverage = a;    }    public String getName(){ return name;    }    public double getGradePointAverage(){ return gradePointAverage;    }   ...

  • Java programming help My program wont run. could someone tell me why. everything seems correct to...

    Java programming help My program wont run. could someone tell me why. everything seems correct to me... giving me the error: Exception in thread "main" java.lang.NumberFormatException: For input string: "Stud" at java.base/java.lang.NumberFormatException.forInputString(Unknown Source) at java.base/java.lang.Integer.parseInt(Unknown Source) at java.base/java.lang.Integer.parseInt(Unknown Source) at Util.readFile(Util.java:35) at Driver.main(Driver.java:8) _________________________________________________________________________________________________________________________ Program Prompt: Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit...

  • Using Java, I created 3 files, Pizza,PizzaOrder, and PizzaOrder_Demo that I attached down below. But I...

    Using Java, I created 3 files, Pizza,PizzaOrder, and PizzaOrder_Demo that I attached down below. But I cannot compile it. Could you please point out the error.The question is that This programming project extends Q1. Create a PizzaOrder class that allows up to three pizzas to be saved in an order. Each pizza saved should be a Pizza object as described in Q1. In addition to appropriate instance variables and constructors, add the following methods: public void setNumPizzas(int numPizzas)—sets the 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