Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm having issues creating the generic item. If anyone could help me figure it out and explain it it would be great please! A1Work.java public

I'm having issues creating the generic item. If anyone could help me figure it out and explain it it would be great please!

A1Work.java

public class A1Work { /* * Create a generic Item class that works with the commented out components in the A1Driver.java * Grading: * Item class is generic: 1pt * Stores a single object of the generic type: 1pt * Has appropriate setters/getters/constructor/toString/equals: 1pt * All method names used in A1Driver match exactly with Item method names: 1pt */ /* * Grading: * Correctly find Kth largest: 2pt */ public static int kthLargest(int K, int[] arr) { //Find kth largest in array //for example, if there are 10 values in the array and K = 5, find 5th largest of the 10 items in the unsorted array //You may determine the kth largest in any method you choose return 0;//return kth Largest from array of random values } /* * Grading: * Used recursion: 1pt (required for any credit) * Followed formula: 2pt * Correct answer: 1pt * Using built in methods to convert to a binary string will receive zero credit */ public static int binaryOnes(int X) { //MUST USE RECURSION FOR CREDIT! //MUST USE MATH FORMULAS BELOW FOR CREDIT! //X mod 2 tells you if there is a 1 or 0 at that position //run X / 2 through function recursively to find next position return 0;//return the number of 1's in the binary representation of the number X } }

A1Driver.java

import java.util.Comparator;

public class A1Driver {

public static void main(String[] args) { System.out.println("----------"); System.out.println("kthLargest"); System.out.println("----------"); int[] arr = new int[] {1,2,3,4,5,6,7,8,9,10}; int k = 4; int correct = 7; int answer = A1Work.kthLargest(k, arr); System.out.println(((answer==correct)?"Correct":"Wrong") + " for k = "+k+" and correct = "+correct+" and your answer = "+answer); arr = new int[] {10,9,8,7,6,5,4,3,2,1}; k = 4; correct = 7; answer = A1Work.kthLargest(k, arr); System.out.println(((answer==correct)?"Correct":"Wrong") + " for k = "+k+" and correct = "+correct+" and your answer = "+answer); arr = new int[] {1,4,7,10,2,5,8,3,6,9}; k = 3; correct = 8; answer = A1Work.kthLargest(k, arr); System.out.println(((answer==correct)?"Correct":"Wrong") + " for k = "+k+" and correct = "+correct+" and your answer = "+answer); System.out.println("----------"); System.out.println("binaryOnes"); System.out.println("----------"); for(int i = 0; i < 10; i++) { answer = A1Work.binaryOnes(i); correct = Integer.bitCount(i); System.out.println(((answer==correct)?"Correct":"Wrong") + " for i = "+i+" and correct = "+correct+" and your answer = "+answer); }

System.out.println("-----------"); System.out.println("genericItem"); System.out.println("-----------"); //UNCOMMENT TO TEST /** / Item num1 = new Item<>(5); Item num2 = new Item<>(5); Item num3 = new Item<>(50); System.out.println(num1.getThing() + ":" + 5); System.out.println(num2.getThing() + ":" + 5); System.out.println(num3.getThing() + ":" + 50); System.out.println(num1.equals(num2) + ":true"); System.out.println(num1.equals(num3) + ":false"); num3.setThing(10); System.out.println(num3.toString() + ":Item [thing=10]"); /**/ } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions