Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Translate this code from C# to Java. class HelloWorld { static void Main() { List packets = new List (); packets.Add(1); packets.Add(2); packets.Add(3); packets.Add(4); packets.Add(5);

Translate this code from C# to Java.

class HelloWorld { static void Main() { List packets = new List(); packets.Add(1); packets.Add(2); packets.Add(3); packets.Add(4); packets.Add(5); // packets.Add(89); packets.Add(48); packets.Add(14);// packets.Add(4); packets.Add(5); //packets.Add(2); packets.Add(2); packets.Add(1); packets.Add(5); packets.Add(3); //packets.Add(1); packets.Add(89); // packets.Add(48); //packets.Add(14); // packets.Add(1); // I/P - 1,2,3,4,5, channels - 2 ---> O/P - 7 // I/P - 2,2,1,5,3, channels - 2 ---> O/P - 7 // I.P - 89,48,14 channels = 3 ---> O/P - 151 int channels = 2; long result = maximumQuality(packets, channels); Console.WriteLine(result); } private static long maximumQuality(List packets, int channels) { if (packets == null || packets.Count() == 0) return 0L; if (packets.Count() == 1) return (long) packets[0]; if (channels <= 0) { // TODO.. throw exception, bad input } packets = packets.OrderBy(x => x).ToList(); int n = channels - 1; int median = 0; while (n-- > 0) { int val = packets[packets.Count()-1]; median += val; packets.RemoveAt(packets.Count()-1); } long result = findMedian(packets, median); return result; } private static long findMedian(List list, long median) { long val = 0; if (list.Count() %2 == 0) { // val = (long) (list[list.Count()/2] + list[list.Count()/2 - 1])/2; val = (long) Math.Ceiling((((double)list[list.Count()/2] + (double)list[list.Count()/2 - 1]))/2); } else { val = (long) list[list.Count()/2]; } return (long) (val + median); } }

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

=+2. Why is due process important?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago