[ad_1]
CS1 Java Programming Short Answer Questions
Q5.
1.Write the header for a method called “displayName” that returns void and takes a String argument called “name”. ONLY WRITE THE HEADER. DO NOT WRITE THE BODY.
Write a single line of code to “call” the “void” method “displayName” from the previous problem. Pass your own name as the argument.
3.Write the header for a method called “sum3” that returns a double and accepts three double values as arguments. WRITE ONLY THE HEADER. DO NOT WRITE ANY OTHER PART OF THE METHOD.
4.Write one or two lines of code that uses the “sum3” method of the previous problem to add the values 1.1, 2.2, and 3.3 and assign the result to a double variable called result. Use at most two lines of code.
5.Write a complete method called “getAreaForRectangle” that returns a double value and accepts a double parameter called width and a double parameter called height. Inside the body of the method, write code to compute and return the area of a rectangle using the width and height parameters. CS1 Java Programming Short Answer Questions
When a primitive data type argument is passed to a method…
Its value is copied into the methods parameter variable
The arguments may be changes within the called method
both
Q6.
Consider the following UML diagram. What is the name of the class?
Given the UML diagram above, list all of the fields that are type String
Given the UML diagram above, list all of the public methods that take no arguments.
Given the UML diagram above, list all of the methods that take at least one argument have a return type void.
Given the UML diagram above, write the complete code for the constructor including both a header and the body. (ref. pp. 339-340 and class notes for examples)
Inside the body of the constructor initialize the title field using the t parameter. Initialize the author field with the a parameter. Initialize the publisher field with the p parameter and initialize the copiesSold field with the cparameter.
Q.7
Write a complete Java statement to declare an integer array named “numbers” with 10 elements
Write a statement to set the fourth element of the “numbers” array to a value of 30
Refer to page 415, then consider the following statements.
int[] numbers1 = {1, 3, 6, 9};
int[] numbers2 = {2, 4, 6, 8};
int result;
Write a statement that multiplies element 0 (i.e., the first element0 of the numbers1 array by element 3 (i.e., the fourth element) of the numbers2 array and assigns the result to the “result” variable. You don’t need to re-declare the variable “result” since it is already declared in the question.
Review page 416, then given the following declaration for an array of integers
int[] numbers = {3,6,9,12,15};Use an Enhanced For loop to print all values of the array to the console
Review the code at the top of page 422 then consider the following two arrays.
int[] a = {1, 2, 3, 4, 5, 6, 7};
int[] b = new int[7];
Use a for loop to write code to copy the contents of array a in to b.
Write the header for a public method called getScores that returns an array of doubles and takes one intargument called count.
Write a Java statement to create an array of String objects called emperors initialized with the names “Octavian”, “Tiberius”, “Gaius”, and “Claudius
Create an array of String objects that can contain 1000 String elements. Use an int constant called SIZE to set the size of the array
Given an array of String objects called “names” with 100 elements, write a single line of code to print out the tenth element to all upper case
Write two lines of code needed to create an array of Rectangle objects called “rectangles” that has room for 100 elements. (Page 444 for similar example). On the first line create an integer constant called NUM_RECTANGLES and set it to 100; On the second line declare the array of rectangle objects using the NUM_RECTANGLES constant.
Write a declaration for a two dimensional array of doubles called temperatures with 12 rows and 31 columns
Store the value 123.4 at row 3 (4th row) column 7 (8th column) of the temperatures array.
Write a statement that creates an ArrayList object and assigns its address to a variable named lizards. The ArrayList should be able to store String objects.
How do you add items to an ArrayList object? (page 473) Give an example of adding a value to the “lizards” ArrayList.
How do you determine an ArrayList object’s size? (page 473) Give an example by printing out the size of the lizards array using System.out.println or similar method.
How do you retrieve a specific item from an ArrayList object? (page 473). Give an example by writing code to print out the item at index 4 from the lizards array.
Write some code to use the enhanced for loop to print out the names in the lizards array
How do you remove an item from an ArrayList object? (page 476) Give an example by removing the item at index 1 of the lizards array.
How do you insert an item at a specific location in an ArrayList object? (page 477). Give an example by inserting a new lizard name at index 3.
How do you replace an item at a specific index of an ArrayList? (page 478). Give an example by setting the value of element at index 7 of the lizards array to “Larry”
Demonstrate how to declare and create an ArrayList of BankAccount objects that can hold 100 elements. (See page 479)
CS1 Java Programming Short Answer Questions
Need Help with a similar Assignment?
What Students Are Saying About Us
.......... Customer ID: 12*** | Rating: ⭐⭐⭐⭐⭐"Honestly, I was afraid to send my paper to you, but splendidwritings.com proved they are a trustworthy service. My essay was done in less than a day, and I received a brilliant piece. I didn’t even believe it was my essay at first 🙂 Great job, thank you!"
.......... Customer ID: 14***| Rating: ⭐⭐⭐⭐⭐
"The company has some nice prices and good content. I ordered a term paper here and got a very good one. I'll keep ordering from this website."