Question

Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in...

Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in one of two ways. In one method, the array elements are placed in a list enclosed in curly braces after the array name definition. For example, the code below creates an array of ints with 3 elements: 1, 2 and 3. int[] a = {1, 2, 3, 4}; We can also initialize an array with a new construct, indicating how many elements we want with a number inside square brackets, as in the example double[] b = new double[43]; When this type of initialization is used, the values of the array are initialized to zero for numeric types, false for booleans, and null for object types (such as String).

Q2) What values would the elements of the following array have initially? int[] q = new int[99];

Q3) What values would the elements of the following array have initially? String[] s = new String[10]; Iterating Through Arrays Arrays in Java use zero-based indexing. You can get how many elements are in an array by using the .length property. To get the length of an array s, you would write s.length. Note that this is not a method, so you don’t need parentheses. To iterate through an array of data, typically a for loop is used. For example
for(int i=0; i<s.length; ++i){ // process s[i] }

Q4) Write a for loop to add 2 to all the elements of an array called y in lab3answers.txt. One way to instantiate an array is with a new command. Another way is to include a list of items in curly braces. Type in or copy the following program and save it in the file

public class Lab3a {

public static void main(String [] args)

{ int a[] = {0,1,2,3};

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

{ System.out.println("a["+i+"] = "+a[i]); }

}

}

Compile and run the program.

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

Q1. Declaration of an array of double

double[] myDoubles = new double[50];

Q2. The elements of the array have zeros initially.

Q3. For a string array the initial values will be null.

Q4.

public class Lab3a {

public static void main(String [] args)

{

int a[] = {0,1,2,3};

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

{

a[i] += 2;

System.out.println("a["+i+"] = "+a[i]); }

}

}

38 dHpp a[0] = 2 a[1] = 3 a[2] =

Let me know if you have any queries or clarifications.... :)

Add a comment
Know the answer?
Add Answer to:
Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in...
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
  • I’m giving you code for a Class called GenericArray, which is an array that takes a...

    I’m giving you code for a Class called GenericArray, which is an array that takes a generic object type. As usual this is a C# program, make a new console application, and for now you can stick all of this in one big file. And a program that will do some basic stuff with it Generic Array List What I want you to do today: Create methods for the GenericArray, Easy(ish): public void Append (T, value) { // this should...

  • How can i print the array in VehicleTest.java with out the hashcode behind the array element?...

    How can i print the array in VehicleTest.java with out the hashcode behind the array element? Vehicle.java public abstract class Vehicle { // class that can describe many vehicle types abstract public double GetCarbonFootPrint(); } Car.java public class Car extends Vehicle{ private double gallon; public Car() { this.gallon = 0;// set gallon to 0 } public Car(double gallon) { this.gallon = gallon; } @Override public double GetCarbonFootPrint() { return (this.gallon * 20);// carbon footprint is #gallons*20 } } Bicycle.java public...

  • Arrays in Functions 2. Arrays in Functions You can use both the array index variables and...

    Arrays in Functions 2. Arrays in Functions You can use both the array index variables and the entire array itself as arguments to functions. The following program updates the elements of the array grades, one-by-one. Thus, we have used a call-by-reference-ish mechanism to update the values (although no & was used). Note that there is a major difference between the grade in the function call and the one in the function definition. In the statement get_grade (grades[i]); "grades" is the...

  • DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an...

    DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an unknown number of cells filled with data. As before, the array will already be filled with strings, some of which contain the string "Angela". You will create a method called countItUp which returns and integer array containing the number of times each of the letters in my name occur within the strings within this array. For example, if the input array was wordFun =...

  • DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an...

    DESCRIPTION: You will be given a 1-D array, called wordFun, of Strings. The array has an unknown number of cells filled with data. As before, the array will already be filled with strings, some of which contain the string "Angela". You will create a method called countItUp which returns and integer array containing the number of times each of the letters in my name occur within the strings within this array. For example, if the input array was wordFun =...

  • Parallel Arrays Summary In this lab, you use what you have learned about parallel arrays to...

    Parallel Arrays Summary In this lab, you use what you have learned about parallel arrays to complete a partially completed Java program. The program should either print the name and price for a coffee add-in from the Jumpin’ Jive coffee shop or it should print the message: "Sorry, we do not carry that.". Read the problem description carefully before you begin. The data file provided for this lab includes the necessary variable declarations and input statements. You need to write...

  • Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized...

    Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized with the following strings: “Einstein”, “Newton”, “Copernicus”, and “Kepler”. 2. Assume names in an Integer array with 20 elements. Write a pseudocode algorithm a For Loop that displays each element of the array. 3. Assume the arrays numberArray1 and numberArray2 each have 100 elements. Write a pseudocode algorithm that copies the values in numberArray1 to numberArray2 . 4. Write a pseudocode algorithm for a...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • Introduction to Arrays. Please make sure program compiles :) int numlist[8]; // declare int array that...

    Introduction to Arrays. Please make sure program compiles :) int numlist[8]; // declare int array that can store 8 values If we wish to fill the array numlist with the integers typed from the keyboard, you can use a for loop. Here is a for loop that will allow you to enter 8 values from the keyboard and will store them in the array numlist. Notice that we have used the variable i as an index for array numlist for...

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