Question

I need help answering a question in JAVA. Also could you explain exactly what is be...

I need help answering a question in JAVA. Also could you explain exactly what is be asked from this question. I will be including the driver class and one function that needs to be answered.


import java.util.Arrays;

public class A2Driver {

   public static void main(String[] args) {
       for(int i = 10; i <= 50; i+=10)
       {
           System.out.println("N = "+i);
           System.out.println("Sum1:"+A2Work.sum1(i));
           System.out.println("Sum2:"+A2Work.sum2(i));
           System.out.println("Sum3:"+A2Work.sum3(i));
           System.out.println("Sum4:"+A2Work.sum4(i));
           System.out.println("Sum5:"+A2Work.sum5(i));
           System.out.println("Sum6:"+A2Work.sum6(i));
           System.out.println();
       }
       for(int i = 10; i <= 50; i+=10)
       {
           System.out.println("N = "+i);
           System.out.println(Arrays.toString(A2Work.algorithm1(i)));
           System.out.println(Arrays.toString(A2Work.algorithm2(i)));
           System.out.println(Arrays.toString(A2Work.algorithm3(i)));
           System.out.println();
       }
   }

----------------------------------------------------------------------------------------------------------------------------------------------------

Question that needs to be answered and explained. Function that returns sum1


   * NOTE: your f(N) does not need to produce the exact answer as your op count for all sums, but it should be close
   */
  
   /*
   * Grading:
   * Add operation counts - 0.5pt
   * f(N) formula (show your work) - 0.5pt
   * O(N) reduction - 0.5pt
   */
   public static long sum1(int N)//f(N) = ; O(N) =
   {
       long opCount = 0;
       long sum = 0;
       for(int i = 0; i < N; i++)
       {
           sum++;
       }
       System.out.println("f(N) = [f(N)=N*(N-1)/2]");
       System.out.println("O(N) = [O(N)=O(N^2)]");
       System.out.println("OpCount : "+opCount);
       return sum;
   }

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

CODE

/*

* Grading:

* Add operation counts - 0.5pt

* f(N) formula (show your work) - 0.5pt

* O(N) reduction - 0.5pt

*/

public static long sum1(int N)//f(N) = ; O(N) =

{

long opCount = 0;

long sum = 0;

for(int i = 0; i < N; i++)

{

sum++;

opCount ++;

}

System.out.println("f(N) = N");

System.out.println("O(N) = O(N)");

System.out.println("OpCount : "+opCount);

return sum;

}

Add a comment
Know the answer?
Add Answer to:
I need help answering a question in JAVA. Also could you explain exactly what is be...
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
  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

    Computer Science - Java Explain the code step by step (in detailed order). Also explain what the program required. Thanks 7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • Need help with this binary tree program. I will give a thumbs up for anybody who...

    Need help with this binary tree program. I will give a thumbs up for anybody who helps. Source Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class Trees { public static int[] prepareData(int[] data){ /* Data is prepared by inserting random values between 1 and data.length. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ ArrayList list = new ArrayList(); for (int i=0; i< data.length; i++) {...

  • help me with a question in java: class A { int i = 10; } class...

    help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • I am in an entry-level Java class and need assistance solving questions 20-25 for a homework...

    I am in an entry-level Java class and need assistance solving questions 20-25 for a homework assignment. Any help is appreciated. Thank you. Q20 Read the code and answer the question 6 Points Read each of the following code snippets and determine if they are equivalent. I.e. they produce the same output. Q20.1 Are these snippets equivalent? 2 Points Snippet 1: int i=0; while (i<10) System.cut.print(i); i++; 1 Snippet 2: for(int i-0; i<10; i++) System.out.print(i); } Yes O No Q20.2...

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