Question

In: Computer Science

Given the following Java source code fragment double price[]; price = new double[16]; What's the name...

Given the following Java source code fragment double price[]; price = new double[16]; What's the name of the array? How many elements are in the array? What's the index of the first element in the array? What's the index of the last element in the array? What's the value of the element at index 3 after the last statement executes?

Solutions

Expert Solution

1. Answer:

Name of the array is price.

2. Answer:

The size of array describes the number of elements in the array.

Here, the size is declared in [ ] square brackets & the size is 16. Hence, it can hold 16 elements.

3.Answer:

Index is the address of the elements stored in an array.

Indexing always starts from zero (0)

Hence, the index of the first element is 0. (zero).

4. Answer:

The last index is size of the array minus 1.(Since, indexing starts from zero).

Therefore, the index of last element in the array is 15 .(16-1=15).

5. Answer:

Initially, since no values are assigned to the array, the default values for the "double" data type is assigned. Hence upon trying to print the value at index 3, use the following java code :

System.out.println(price[3]);

It will print 0.0 as output.

==============END OF THE ANSWER==============


Related Solutions

Determine the output using C++ of the following code fragment i) double *pt; double a[3] =...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] = {1.2, 2.3, 3.4}; pt = &a[1]; pt += 1; cout << *pt << endl; ii) int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { cout << num << "bb"; num *= 3; } cout << endl; i++; }
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;...
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;     int quantity;     bool equals(const Product&); }; how would you define the equals function so two products are equal if their names and prices are equal? a. bool equals(const Product& to_compare) {     return (name == to_compare.name && price == to_compare.price); } b. bool Product::equals(const Product& to_compare) {     return (name == to_compare.name || price == to_compare.price); } c. bool equals(const Product& to_compare)...
Given the following code fragment: x = (1,2,3) Match the syntax with the action: value =...
Given the following code fragment: x = (1,2,3) Match the syntax with the action: value = x{0} (a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable value = x(0) (a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable value = x[0] (a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable
Implement Java source code for a class called Temperature that does the following: a. Prompts the...
Implement Java source code for a class called Temperature that does the following: a. Prompts the user to input a temperature b. Prints “Too Hot!” when the input temperature is above 75 c. Prints “Too Cold!” when the input temperature is below 40 d. Prints “Just Right!” when the input temperature is 40 to 75 e. Be precise, include comments, prologue, etc. if needed.
in java my code double price[] = {26.99, 22.99, 13.99, 56.99, 38.99}; // Variable Declaration Scanner...
in java my code double price[] = {26.99, 22.99, 13.99, 56.99, 38.99}; // Variable Declaration Scanner keyIn = new Scanner(System.in); //Header System.out.println( "----------------------------------------\n" + " Grocery Shop Price Calculator \n" + "----------------------------------------\n") ; System.out.print ("Please enter the quantities for each item in the list? "); double totalCost = 0; double fishAmount = 0; for(int i=0; i<price.length; i++) { int items = Integer.parseInt(keyIn.nextLine()); if(i==4) fishAmount = price[i]*items; else totalCost = totalCost + price [i]*items; } System.out.print("Do you have the membership(Y/N) ");...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code. int counter; int x = 5; int y = 6; for (counter =10; counter >0;counter--) IF(X==Y) Y = Y + 1 ; ELSE Y = Y + 2} Fill in the blanks in the following code: MOV__________ ;loop counter into r0-ten times round the loop MOV__________ ;Value of y loaded into r1 MOV__________ ;Value of x...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
Fix the following java code package running; public class Run {    public double distance; //in...
Fix the following java code package running; public class Run {    public double distance; //in kms    public int time; //in seconds    public Run prev;    public Run next;    //DO NOT MODIFY - Parameterized constructor    public Run(double d, int t) {        distance = Math.max(0, d);        time = Math.max(1, t);    }       //DO NOT MODIFY - Copy Constructor to create an instance copy    //NOTE: Only the data section should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT