Java Computer Programming, make the deliverables fit into one program

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Modularization Techniques
Section: Chapter Questions
Problem 2GZ
icon
Related questions
Question

Java Computer Programming, make the deliverables fit into one program.  

An array is a consecutive storage for multiple items of the same type. You can store a value in an
array using an index (location in the array). You can get a value from an array using an index.
An array is like a row of lockers, except that you can't cram lots of stuff into it. You can only store
one value at each array index. An array index is like a locker number. It helps you find a particular
place to store your stuff and retrieve stuff.
There are several ways to declare and create a one-dimensional array. An array named numbers
is being instantiated in the following statements:
int[] numbers = new int[5];
and
int[] numbers = {11, 12, -33, 54, 5};
Unless initialized, the values in an array of int's or double's are automatically initialized to zero.
The value stored in an index position of an array is called an element. The first element of an
array is considered to be stored in index position zero (aka subscript position zero).
To assign or change a value stored in the array, you can use an assignment statement like this
numbers [0] = 99; //which stores the value 99 in index position 0.
To display an element in an array, you can use the square brackets as in
System.out.println(numbers [0]); //to make the value 99 appear.
The length property of an array can be used as in
int[] scores =
{88, 77, 62, 98, 70};
System.out.println (scores.length);
which would printout the length of the array named scores which is 5 even though the value 70
is stored in index position 4.
Note that length is a public property and not a method. Therefore, parentheses must not be used.
Do not confu:
this public property with the public method that is available in the String class.
Note the difference in the following statements:
int[] scores
{88, 77, 62, 98, 70};
System.out.println("The length of this array is
+ scores.length);
and
String name = "Bob";
System.out.println("The length of this string is "
+ name.length ());
Once an array has been instantiated, its length can't be changed. You can expand an array
indirectly though by creating a bigger array and then copying the contents of the original array
into the bigger array.
Transcribed Image Text:An array is a consecutive storage for multiple items of the same type. You can store a value in an array using an index (location in the array). You can get a value from an array using an index. An array is like a row of lockers, except that you can't cram lots of stuff into it. You can only store one value at each array index. An array index is like a locker number. It helps you find a particular place to store your stuff and retrieve stuff. There are several ways to declare and create a one-dimensional array. An array named numbers is being instantiated in the following statements: int[] numbers = new int[5]; and int[] numbers = {11, 12, -33, 54, 5}; Unless initialized, the values in an array of int's or double's are automatically initialized to zero. The value stored in an index position of an array is called an element. The first element of an array is considered to be stored in index position zero (aka subscript position zero). To assign or change a value stored in the array, you can use an assignment statement like this numbers [0] = 99; //which stores the value 99 in index position 0. To display an element in an array, you can use the square brackets as in System.out.println(numbers [0]); //to make the value 99 appear. The length property of an array can be used as in int[] scores = {88, 77, 62, 98, 70}; System.out.println (scores.length); which would printout the length of the array named scores which is 5 even though the value 70 is stored in index position 4. Note that length is a public property and not a method. Therefore, parentheses must not be used. Do not confu: this public property with the public method that is available in the String class. Note the difference in the following statements: int[] scores {88, 77, 62, 98, 70}; System.out.println("The length of this array is + scores.length); and String name = "Bob"; System.out.println("The length of this string is " + name.length ()); Once an array has been instantiated, its length can't be changed. You can expand an array indirectly though by creating a bigger array and then copying the contents of the original array into the bigger array.
Deliverables:
1) Write a static method named computeOddSum that is passed an array of int's named
numbers. The method must compute and return the sum of all the values in numbers that
are odd. For example, if numbers is {3, 10, 11, 2, 6, 9, 5} then the value 28 should be
returned since 3 + 11 + 9 + 5 = 28 is the sum of all the odd values in numbers.
2) Write a static method named replaceEvens that receives the parameter numbers which
is an array of int's. The method must replace all elements of numbers that store even
values with the negative of that value. That is, if the number 12 is found in a given position
of numbers, then the method must replace the 12 with a -12. You can assume that all
values in the array are positive numbers greater than zero.
3) Write a static, void method named replaceAll that accepts an array of int's named
numbers as well as an int named num. The method must replace all occurrences of num
that are found in even-numbered index positions with the value of 3. That is, if the value
num is stored in numbers[4] then it must be replaced with the value 3 since 4 is an even
number. But if num is found in numbers[5], it must not be replaced since 5 is not an even
number. For this exercise, zero is considered to be an even number. The method must
work for an array of any size.
4) There is a non-empty array of String's named names. Write a code segment that removes
the last letter of the String stored in the very last position of names. For bragging rights
and if possible (and I'm not sure if it is), write a single statement that performs this task.
Transcribed Image Text:Deliverables: 1) Write a static method named computeOddSum that is passed an array of int's named numbers. The method must compute and return the sum of all the values in numbers that are odd. For example, if numbers is {3, 10, 11, 2, 6, 9, 5} then the value 28 should be returned since 3 + 11 + 9 + 5 = 28 is the sum of all the odd values in numbers. 2) Write a static method named replaceEvens that receives the parameter numbers which is an array of int's. The method must replace all elements of numbers that store even values with the negative of that value. That is, if the number 12 is found in a given position of numbers, then the method must replace the 12 with a -12. You can assume that all values in the array are positive numbers greater than zero. 3) Write a static, void method named replaceAll that accepts an array of int's named numbers as well as an int named num. The method must replace all occurrences of num that are found in even-numbered index positions with the value of 3. That is, if the value num is stored in numbers[4] then it must be replaced with the value 3 since 4 is an even number. But if num is found in numbers[5], it must not be replaced since 5 is not an even number. For this exercise, zero is considered to be an even number. The method must work for an array of any size. 4) There is a non-empty array of String's named names. Write a code segment that removes the last letter of the String stored in the very last position of names. For bragging rights and if possible (and I'm not sure if it is), write a single statement that performs this task.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Introduction to computer system
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,