Question

import java.util. Scanner; public class HM5 publi int end 1list. length; for (int i 0; i < end; i++) if (list[i] = list[j]) { In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you.

1-First exercise

import java.util.Scanner;
public class first {
public static int average(int[] array){
   int total = 0;
   for(int x: array)
       total += x;
   return total / array.length;
   }
public static double average(double[] array){
   double total = 0;
   for(double x: array)
       total += x;
   return total / array.length;
   }
public static void main(String args[]){
   Scanner sc = new Scanner(System.in);
   double[] d = new double[10];
   System.out.println("Enter 10 double values: ");
   for(int i=0; i<10; i++)
       d[i] = sc.nextDouble();
   System.out.println("Average = " + average(d));
   }
}

2-Second exercise

import java.util.Scanner;
public class second {
   public static int[] eliminateDuplicates(int[] list){
       int end = list.length;
       for (int i = 0; i < end; i++){
           for (int j = i + 1; j < end; j++){
               if (list[i] == list[j]){
                   int shiftLeft = j;
                   for (int k = j+1; k < end; k++, shiftLeft++){
                       list[shiftLeft] = list[k];
                       }
                   end--;
                   j--;
                   }
               }
           }
       int[] outlist = new int[end];
       for(int i = 0; i < end; i++){
           outlist[i] = list[i];
           }
       return outlist;
       }
   public static void main(String[] args){
       Scanner in = new Scanner(System.in);
       int n = 10;
       System.out.println("Enter 10 Elements \n");
       int[] a = new int[n];
       for(int i=0;i<n;i++){
           a[i] = in.nextInt();
           }
       int[] output = eliminateDuplicates(a);
       System.out.println("\nOutput Elements: \n");
       for (int i : output){
           System.out.print(i + " ");
           }
       }
   }

I need to put both these exercises under the same class. But I need them to be called from the main method I think. The idea is to have both of them under the same class. so I can execute each of them.

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

We can solve the above problem in the following way,

  1. We can make the main functions of both the files as the first() and second() function in the final class.
  2. We can call them using the new main function in the final class.

Following is the code for the same.

import java.util.Scanner;

public class HM5 {
   public static int average(int[] array){
   int total = 0;
   for(int x: array)
   total += x;
   return total / array.length;
   }
   public static double average(double[] array){
   double total = 0;
   for(double x: array)
   total += x;
   return total / array.length;
    }
   public static void first(){
   Scanner sc = new Scanner(System.in);
   double[] d = new double[10];
   System.out.println("Enter 10 double values: ");
   for(int i=0; i<10; i++)
   d[i] = sc.nextDouble();
   System.out.println("Average = " + average(d));
   }
   public static int[] eliminateDuplicates(int[] list){
int end = list.length;
for (int i = 0; i < end; i++){
for (int j = i + 1; j < end; j++){
if (list[i] == list[j]){
int shiftLeft = j;
for (int k = j+1; k < end; k++, shiftLeft++){
list[shiftLeft] = list[k];
}
end--;
j--;
}
}
}
int[] outlist = new int[end];
for(int i = 0; i < end; i++){
outlist[i] = list[i];
}
return outlist;
}
   public static void second(){
Scanner in = new Scanner(System.in);
int n = 10;
System.out.println("Enter 10 Elements \n");
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = in.nextInt();
}
int[] output = eliminateDuplicates(a);
System.out.println("\nOutput Elements: \n");
for (int i : output){
System.out.print(i + " ");
}
}
   public static void main(String args[]) {
       System.out.println("Running the First Program");
       first();
       System.out.println("Running the Second Program");
       second();
   }

}

A snippet of the sample output:

kterminated> HM5 [Java Application] C:\Program Files\Java\jre1.8.0_171\bin\javaw.exe (Jun 26, 2019, 2:31:47 AM) Running the F

That was a nice question to answer
Friend, 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 if you think effort deserves a like.
Thanks

Add a comment
Know the answer?
Add Answer to:
In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...
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
  • 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] -...

  • 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...

  • I have a program that reads a file and then creates objects from the contents of...

    I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...

  • I need to change the following code so that it results in the sample output below...

    I need to change the following code so that it results in the sample output below but also imports and utilizes the code from the GradeCalculator, MaxMin, and Student classes in the com.csc123 package. If you need to change anything in the any of the classes that's fine but there needs to be all 4 classes. I've included the sample input and what I've done so far: package lab03; import java.util.ArrayList; import java.util.Scanner; import com.csc241.*; public class Lab03 { public...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

  • /** this is my code, and I want to invoke the method, doubleArraySize() in main method....

    /** this is my code, and I want to invoke the method, doubleArraySize() in main method. and I need to display some result in cmd or terminal. also, I have classes such as Average and RandomN needed for piping(pipe). please help me and thank you. **/ import java.util.Scanner; public class StdDev { static double[] arr; static int N; public static void main(String[] args) { if(args.length==0){ arr= new double[N]; Scanner input = new Scanner(System.in); int i=0; while(input.hasNextDouble()){ arr[i] = i++; }...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

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