Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Question
Book Icon
Chapter 17, Problem 7PC
Program Plan Intro

Generic class to find out largest, smallest, total and average of elements in an ArrayList

Program plan:

  • Import the required package.
  • Create the class “MyList” with a type parameter “T” that is constrained to an upper bound Number using the keyword extends,
    • Create “ArrayList” as a field of class “MyList” with type parameter “T”.
    • Define the constructor.
    • Define the public method “add()” to accept the parameter of type “T”,
      • Store the value in “ArrayList” using the reference variable “list”.
    • Define the method “largest()”,
      • Call the method “get()” to store the initial value of “ArrayList” in the variable.
      • Iterate the elements of “ArrayList” using “for” loop and compare each value using if statement to find the largest value in the “ArrayList”.
      • Return the largest value using the keyword “return”.
    • Define the method “smallest()”,
      • Call the method “get()” to store the initial value of “ArrayList” in the variable.
      • Iterate the elements of “ArrayList” using “for” loop and compare each value using if statement to find the smallest value in the “ArrayList”.
      • Return the smallest value using the keyword “return”.
    • Define the method “Total()”,
      • Call the method “get()” to store the initial value of “ArrayList” in the variable.
      • Iterate the elements of “ArrayList” using “for” loop and calculate the total value of elements in the “ArrayList”.
      • Return the total value using the keyword “return”.
    • Define the method “Avg()”,
      • Call the method “get()” to store the initial value of “ArrayList” in the variable.
      • Iterate the elements of “ArrayList” using “for” loop and calculate the total value of elements in the “ArrayList”.
      • Calculate and return the average value using the keyword “return”.
  • Create the class “Main”,
    • Define the method “main()”,
      • Create the object of a class “MyList” as a type of “Integer”.
      • Call the “add()” method to fill the values in the “ArrayList”.
      • Call the method “largest()” to print the largest value of “ArrayList”.
      • Call the method “smallest()” to print the smallest value of “ArrayList”.
      • Call the method “Total()” to print the total value of elements in the “ArrayList”.
      • Call the method “Avg()” to print the average value of elements in the “ArrayList”.

Blurred answer
Students have asked these similar questions
Library class: Fields: books as arraylist to store all the books available in the library B3 B1 B2 В4 B5 Constructor: Library() } : No parameterized constructor to create the books object Methods: 1. addBook(Book bookobj): to add a new book to the arraylist; send the book object as a parameter to this method. 2. searchBook(String name): search a book by a given name in the arraylist. if exits print bookX found; otherwise print not found!!! 3. RemoveBook(String name): to delete a book object from arraylist by a given name: Hint: you need to use iterator class and remove method. Book class: Fileds: bookld, bookname. Constructor: two parameterized constructor that accepts name a parameter Book(int ID, String name) ..... Methods: 1. Accessor method to return the name of the book 2. Mutator method to change the id of the book. Use this Main method: in your program: public static void main(String args[]) { Library lib=new Library(); lib.addBook(new Book("b1")); lib.addBook(new Book("b2"));…
Make Album in c++ You are going to develop a new class named Album. This class will has (at a minimum) teh following attributes and features: Attributes: Album Title Artist Name Array of song names (Track Listing) Array of track lengths (decide on a unit) At most there can be 20 songs on an album. Behaviors: Default and at least one argumented constructor Appropriate getters for attributes bool addTrack(string name, type duration) - adds track to album. You decide type on duration. Make sure you document what dis is. getAlbumLength() - method dat returns total length of album matching watever units / type you decide. getAlbumLength_string() - method dat returns total length of album in mm:ss format string. I would suggest using you're other getLength function here. string shuffleAlbum(int n) - method dat returns a formatted string made up of n random tracks from teh album. The string will be in the format [1] track_name (mm:ss), [2] track_name (mm:ss)... Well formatted print()…
Make Album in c++ You are going to develop a new class named Album. This class will has (at a minimum) teh following attributes and features: Attributes: Album Title Artist Name Array of song names (Track Listing) Array of track lengths (decide on a unit) At most there can be 20 songs on an album. Behaviors: Default and at least one argumented constructor Appropriate getters for attributes bool addTrack(string name, type duration) - adds track to album. You decide type on duration. Make sure you document what dis is. getAlbumLength() - method dat returns total length of album matching watever units / type you decide. getAlbumLength_string() - method dat returns total length of album in mm:ss format string. I would suggest using you're other getLength function here. string shuffleAlbum(int n) - method dat returns a formatted string made up of n random tracks from teh album. The string will be in the format [1] track_name (mm:ss), [2] track_name (mm:ss)... Well formatted print()…

Chapter 17 Solutions

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Ch. 17.3 - Prob. 17.11CPCh. 17.3 - Prob. 17.12CPCh. 17.3 - Prob. 17.13CPCh. 17.3 - Prob. 17.14CPCh. 17.4 - Prob. 17.15CPCh. 17.5 - Prob. 17.16CPCh. 17.5 - Prob. 17.17CPCh. 17.6 - Prob. 17.18CPCh. 17.6 - Prob. 17.19CPCh. 17.6 - Prob. 17.20CPCh. 17.8 - Prob. 17.21CPCh. 17.8 - Prob. 17.22CPCh. 17.9 - Prob. 17.23CPCh. 17.9 - During the process of erasure, when the compiler...Ch. 17.9 - Prob. 17.25CPCh. 17 - Prob. 1MCCh. 17 - Prob. 2MCCh. 17 - Look at the following method header: void...Ch. 17 - Look at the following method header: void...Ch. 17 - Look at the following method header: void...Ch. 17 - Look at the following method header: void...Ch. 17 - Prob. 7MCCh. 17 - Prob. 8MCCh. 17 - Prob. 9MCCh. 17 - The process used by the Java compiler to remove...Ch. 17 - True or False: It is better to discover an error...Ch. 17 - Prob. 12TFCh. 17 - True or False: Type parameters must be single...Ch. 17 - Prob. 14TFCh. 17 - Prob. 15TFCh. 17 - True or False: You cannot create an array of...Ch. 17 - Prob. 17TFCh. 17 - Prob. 18TFCh. 17 - Prob. 1FTECh. 17 - Assume the following is a method header in a...Ch. 17 - public class MyClassT { public static void...Ch. 17 - public class PointT extends Number super Integer {...Ch. 17 - Assume there is a class named Customer. Write a...Ch. 17 - Assume names references an object of the...Ch. 17 - Prob. 3AWCh. 17 - Prob. 4AWCh. 17 - Prob. 5AWCh. 17 - Prob. 6AWCh. 17 - Prob. 7AWCh. 17 - Prob. 1SACh. 17 - Look at the following method header: public T...Ch. 17 - Prob. 3SACh. 17 - Do generic types exist at the bytecode level?Ch. 17 - Prob. 5SACh. 17 - When the compiler encounters a class, interface,...Ch. 17 - Prob. 1PCCh. 17 - Prob. 2PCCh. 17 - Prob. 3PCCh. 17 - Prob. 4PCCh. 17 - Prob. 5PCCh. 17 - Prob. 6PCCh. 17 - Prob. 7PC
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education