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.
We can solve the above problem in the following way,
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](http://img.homeworklib.com/images/41487c89-e105-4f43-93a6-dc163d7f22f5.png?x-oss-process=image/resize,w_560)
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
In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...
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] -...
import java.util.Scanner; public class Chpt7_Project { public static void bubbleSort(int[] list) { int temp; for (int i = list.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } ...
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 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...
JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; } public static void main(String[] args) { int n, i, k; System.out.println("Enter...
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 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.
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++;
}...
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 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...