Question

In: Computer Science

Java Programming Preferably Concepts: Generics Arrays Objects Part I: Write a routine in Java that takes...

Java Programming Preferably

Concepts:

Generics

Arrays

Objects

Part I: Write a routine in Java that takes an array, the length of the array, and an element and returns the position of the element in the array. For example, given the array with values [2, 4, 6, 8] and element 6, the routine should return 2 (since counting from 0, 6 occurs at position 2 in the array). Your routine should use generics to enable your routine to be reusable for different element types. Be sure to test your code with a Java compiler before you submit it.

Part II: Write a generic “greater-than” function that (a) takes two objects as arguments, each of which has a “value” method which returns an integer; and (b) returns the argument whose “value” method returns the larger integer. Your generic function should constrain its type argument so that types without a “value” method cannot be used.Please review the test harness example to ensure your program meets the requirements. Find the test harness below;

public class TestGenerics {

public static void main(String[] args) {

MyFirstObject myObj1 = new MyFirstObject();

MySecondObject myObj2 = new MySecondObject();

MyGenerics mg = new MyGenerics();

Integer[] array = {2,4,6,8};

System.out.println( mg.partOne(array, array.length, 6) );

System.out.println( mg.partTwo(myObj1, myObj2) );

}

}

Solutions

Expert Solution

public class MyGenerics<T extends Comparable<T>>{

public int findPosition(T arr[] , int len, T target) {

for(int i=0 ; i<len; ++i) {

if(arr[i].compareTo(target)==0)

return i;

}

return -1;

}

public int greaterthan(Object o1, Object o2) {

MyFirstObject f1 = (MyFirstObject)o1;

MySecondObject f2 = (MySecondObject)o2;

if(f1.getValue() > f2.getValue())

return f1.getValue() ;

return f2.getValue() ;

}

}

============================================================

public class MyFirstObject{

int value;

MyFirstObject(int v){

this.value = v;

}

int getValue() {

return value;

}

}

===========================================

public class MySecondObject{

int value;

MySecondObject(int v){

this.value = v;

}

int getValue() {

return value;

}

}


===================================================

//Changed the function names, So please use this

public class TestGenerics {

public static void main(String[] args) {

MyFirstObject myObj1 = new MyFirstObject(10);

MySecondObject myObj2 = new MySecondObject(20);

MyGenerics mg = new MyGenerics();

Integer[] array = {2,4,6,8};

System.out.println( mg.findPosition(array, array.length, 6) );

System.out.println( mg.greaterthan(myObj1, myObj2) );

}

}

============================================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION...
Use JAVA BASICS  classes, aggregation and manipulating arrays of objects. I DO NOT WANT THE SAME SOLUTION WHICH ALREADY EXISTS ON CHEG. DO NO USE ARRAY LIST Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for each dog: dogId (int) - must be unique name (string) age (double) - cannot be less than 0 or more than 25 breed (string) sex (char) – m...
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
JAVA I need to write a code that calculates mean and standard deviation using arrays and...
JAVA I need to write a code that calculates mean and standard deviation using arrays and OOP. This is what I have so far. I'm close but my deviation calculator method is throwing errors. Thanks so much :) import java.util.Scanner; import java.util.Arrays; public class STDMeanArray { private double[] tenUserNums = new double[10];//Initialize an array with ten index' private double mean; private double deviation; Scanner input = new Scanner(System.in);//create new scanner object /*//constructor public STDMeanArray(double tenUserNum [], double mean, double deviation){...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and...
(java) Part 1 Write a method named compare that accepts two String arrays as parameters and returns a boolean. The method should return true if both arrays contain the same values (case sensitive), otherwise returns false. Note - the two arrays must be the same length and the values must be in the same order to return true. Part  2 Write a method named generateArray that accepts an int size as its parameter and returns an int[] array where each element...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices)...
Lab # 4 Multidimensional Arrays Please write in JAVA. Programming Exercise 8.5 (Algebra: add two matrices) Write a method to add two matrices. The header of the method is as follows: public static double[][] addMatrix(double[][] a, double[][] b In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element cij is aij + bij. For example, for two 3 * 3 matrices...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT