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 ... ").
Points to consider:
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
Java Create a class NumberList that stores a list of integers and a list of doubles....
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...
Create a class called Customer that includes three pieces of information as instance variables - a firstname (type string), a last name (type string) and a credit limit (double) Your class should have a constructor that initializes the three values. a. Provide a customer that initializes these three instance variables. Validate credit limit; it must be greater than 0.0. b. Provide set and get methods for each instance variable. Validate credit limit; it must be greater than 0.0. Write a...
PYTHON LANGUAGE Task 11 -Create a list with 5 items having 3 distinct data types -output the second element of the list -output the last element of the list using -negative index -positive index -len method Task 12 -Create a list with 3 items -add an item to the end of the list -remove the first item of the list -determine if an item exists in the list -if it does, display its index -if it does not, give user...
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, 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...
Write a JAVA program to read a list of nonnegative integers and to display the largest integer, the smallest integer and the average of all the integers. The user indicates the end of the input by entering a negative sentinel value that is not used in finding the largest, smallest and average values. The average should be a value of type double so that it is computed a fractional part. * Java program free from syntax, logic and run time...
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...
JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...
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). 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...