Question

Java Create a class NumberList that stores a list of integers and a list of doubles....

Java

Create a class NumberList that stores a list of integers and a list of doubles. Create a driver class that creates two objects of type NumberList and and ask the user to fill one of these lists. The other list should have integer values 1,2,3,4 and double values 1.1, 2.2, 3.3, 4.4. Declare a winner between the two objects by comparing their integer lists element by element and counting which list has more winning positions (the lists will need to be the same length). Then sum the winning object's double list and declare this sum as its 'house total'. All operations should be performed with methods from the NumberList class.

Your output should be the winning object's list of integers on one line (e.g., '5 4 8 2'), the sum of its double list (e.g., '10.2') and some type of acknowledgement that that object one (e.g., "And the winner is ... ").

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

Points to consider:

  1. Finding the array.
  2. Then find the comparison.
  3. Judge for the winner
  4. Print

Code

import java.util.Scanner;

class NumberList{
   public int a[];
   public double b[];
   public NumberList(int a[], double b[]) {
       this.a = new int[4];
       this.b = new double[4];
       for(int i = 0;i<4;i++) {
           this.a[i] = a[i];
           this.b[i] = b[i];
       }
   }
}
public class Game {
   public static void main(String args[])
   {
       int a[] = new int[] {1,2,3,4};
       double b[] = new double[] {1.1,2.2,3.3,4.4};
       NumberList l1 = new NumberList(a, b) ;
       Scanner scan= new Scanner(System.in);
       System.out.println("Enter the list 4 integer values");
       for(int i = 0;i<4;i++) {
           a[i] = scan.nextInt();
       }
       System.out.println("Enter the list 4 double values");
       for(int i = 0;i<4;i++) {
           b[i] = scan.nextDouble()();
       }
       NumberList l2 = new NumberList(a, b);
       int winl1 = 0, winl2 = 0;
       for(int i = 0;i<4;i++) {
           if(l1.a[i] > l2.a[i]) {
               winl1 ++;
           }else {
               winl2 ++;
           }
           if(l1.b[i] > l2.b[i]) {
               winl1++;
           }else {
               winl2++;
           }
       }
       double sum = 0;
       if(winl1>winl2) {
           System.out.println("Winner list of integer: ");
          
           for(int i=0;i<4;i++) {
               System.out.print(l1.a[i] + " ");
               sum += l1.b[i];
           }
       }else {
           for(int i=0;i<4;i++) {
               System.out.print(l2.a[i] + " ");
               sum += l2.b[i];
           }
       }
       System.out.println("\n"+ sum);
       System.out.println("The winner is list: " + (winl1>winl2?"List 1":"List2"));
   }
}


Output Snippet

Friend, That was a nice question to answer
If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like it if you think effort deserves like.
Thanks

Add a comment
Know the answer?
Add Answer to:
Java Create a class NumberList that stores a list of integers and a list of doubles....
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
  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Implement the following classes in the UML: 1. List class includes:       constructor List(): Create an...

    Implement the following classes in the UML: 1. List class includes:       constructor List(): Create an empty list with a length of 100. Hint: double [] list = new double [100];       constructor List(len: int): Create a List with a user specified maximum length. Hint: double [] list = new double [len];       add: add a new element to the end of the unsorted list.       print: display the list 2.SortedList class includes       two constructors : similar to the...

  • Instructions We're going to create a program which will allow the user to add values to a list, p...

    Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List<double> _values new List<double>) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the...

  • Create a new Java Application that test MyStack by having the following methods in main class:...

    Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute....

    Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute. The constructor will simply create an empty list. This will be a different LinkedList, as all of the items will be in ascending numerical order. That means items will not necessarily be placed at the end of the list, but placed where it should be located. Note that the iterator simply uses the next() method, so if you use the iterator to find the...

  • You are to write a Java Class using Generics. This class is to be a Double...

    You are to write a Java Class using Generics. This class is to be a Double Linked List container for holding other objects which are comparable. The Class should support the following features: Print the elements of the collection in order (ascending). Insert an item. Remove an item. Empty the collection. Find the index of an element, using a binary recursive search. The program should include a driver class (Main) that provides a command line input/output. This driver class should:...

  • In JAVA Create a PrintChar class that implements Runnable. The constructor should accept a character and...

    In JAVA Create a PrintChar class that implements Runnable. The constructor should accept a character and an integer as parameters. The run method should print the character the number of times indicated by the integer. Create an application that instantiates two PrintChar objects, one passed “A” and 200 and one passed “B” and 200. It then instantiates and starts two thread objects, one for each of the two PrintChar objects. Experiment with the resulting system, using different numerical parameters for...

  • In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and...

    In JAVA, please In this module, you will combine your knowledge of class objects, inheritance and linked lists. You will need to first create an object class encapsulating a Trivia Game which INHERITS from Game. Game is the parent class with the following attributes: 1. description - which is a string 2. write the constructor, accessor, mutator and toString methods. Trivia is the subclass of Game with the additional attributes: 1. trivia game id - integer 2. ultimate prize money...

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