Array driver class Java - java

Hey guys I'm having problems figuring out how to write my driver class for my Array class.
My questions
1.How do I make the array in my Array class reference the array in my Driver class?
2.How do I pass my class methods from my Array class to my Driver class so I can print them out?
Here's my two bits of code
Main class:
import javax.swing.*;
import java.util.*;
public class Array
{
double sum = 0;
int max = 0;
int min = numbers[0];
double sd = 0;
int mode = 0;
int modeCount = 0;
public double average()
{
for(int i=0; i<numbers.length; i++)
{
sum = sum + numbers[i];
}
double average = sum / numbers.length;
return average;
}
public int max()
{
for(int i=0; i<numbers.length; i++)
{
if(numbers[i] > max)
{
max = numbers[i];
}
}
return max;
}
public int min()
{
for(int i=0; i<numbers.length; i++)
{
if(numbers[i] < min)
{
min = numbers[i];
}
}
return min;
}
public double standardDeviation()
{
for (int i=0; i<numbers.length;i++)
{
sum = sum + numbers[i];
double average = sum / numbers.length;
{
sd += ((numbers[i] - average)*(numbers[i] - average)) / (numbers.length - 1);
}
}
double standardDeviation = Math.sqrt(sd);
return standardDeviation;
}
public int mode()
{
for (int i = 0; i < numbers.length; ++i)
{
int count = 0;
for (int j = 0; j < numbers.length; ++j)
{
if (numbers[j] == numbers[i]) ++count;
}
if (count > modeCount) {
modeCount = count;
mode = numbers[i];
}
}
return mode;
}
}
Driver class:
import javax.swing.*;
import java.util.*;
public class ArrayTest
{
public static void main(String [] args)
{
int[] numbers;
numbers = new int [20];
Random rand = new Random(2621);
int maxRange = 65;
int minRange = 20;
for(int i=0; i<20; i++)
{
numbers[i] = rand.nextInt(maxRange - minRange + 1) + minRange;
}
Arrays.sort(numbers);
}
}

Looks a lot like a homework assignment...
In your Array class, you can keep a reference to the array and then have a method called setArray() that you can use to pass the array from your main class to your Array class.
int[] numbers;
public void setArray(int[] nums) {
numbers = nums;
}
then from your main class you can call
Array.setArray(myIntArray);
int max = Array.max();
In real life (other than for programming practice), you would never want to do this though.

Related

Is there a way to display the users input back to them before displaying the mean and median?

I am working with the following code that allows the user to enter 9 numbers and it displays the mean and median of the numbers back to the user. I am trying to figure out the best way to revise my code so that before displaying the mean and median it will also show the 9 numbers the user entered. Thanks so much for any help in this matter!
import java.util.*;
class MeanMedian
{
private static float mean(int arr[]){
int n = arr.length;
float sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];
}
return sum / n;
}
public static void selectionSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int lowindex = i;
for (int j = array.length - 1; j > i; j--)
if (array[j] < (array[lowindex]))
lowindex = j;
int temp = array[i];
array[i] = array[lowindex];
array[lowindex] = temp;
}
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static float median(int arr[]){
selectionSort(arr);
if(arr.length%2 == 0){
return arr[(arr.length/2)-1];
}
else{
return arr[arr.length/2];
}
}
public static void main(String args[]){
int n = 9;
Scanner scanner = new Scanner(System.in);
int arr[] = new int[n];
System.out.print("Enter "+ n +" integers: ");
for(int i = 0;i<n;i++){
arr[i] = scanner.nextInt();
}
System.out.println("mean: "+ mean(arr));
System.out.println("median: "+ median(arr));
}
}
I am trying to figure out the best way to revise my code so that
before displaying the mean and median it will also show the 9 numbers
the user entered.
Something like:
// ... previous code ...
System.out.println("You entered: ");
for(int i : arr) {
System.out.println(i);
}
System.out.println("mean: "+ mean(arr));
System.out.println("median: "+ median(arr));
The following code worked perfectly and passed all checks I ran on it :)
import java.util.*;
class MeanMedian
{
private static float mean(int arr[]){
int n = arr.length;
float sum = 0;
for(int i = 0;i<n;i++){
sum += arr[i];
}
return sum / n;
}
public static void selectionSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
int lowindex = i;
for (int j = array.length - 1; j > i; j--)
if (array[j] < (array[lowindex]))
lowindex = j;
int temp = array[i];
array[i] = array[lowindex];
array[lowindex] = temp;
}
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private static float median(int arr[]){
selectionSort(arr);
if(arr.length%2 == 0){
return arr[(arr.length/2)-1];
}
else{
return arr[arr.length/2];
}
}
public static void main(String args[]){
int n = 9;
Scanner scanner = new Scanner(System.in);
int arr[] = new int[n];
System.out.print("Enter "+ n +" integers: ");
for(int i = 0;i<n;i++){
arr[i] = scanner.nextInt();
}
System.out.println("You entered: ");
for(int i : arr) {
System.out.println(i);
}
System.out.println("mean: "+ mean(arr));
System.out.println("median: "+ median(arr));
}
}

Difficulty trying to sort 10 numbers inputted by a user. Must use arrays and a separate method for sorting

My program isn't sorting the numbers at all. It displays them in the order they were initially entered. It must sort them from smallest to largest number. The code below should find the largest number in the array and swap it with the last .the code is below:
import java.util.Scanner;
public class maxSorttt {
public static void main(String[] args) {
double[] ten = new double[10];
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 numbers: ");
for (int i = 0; i < ten.length; i++)
ten[i] = input.nextDouble();
sort(ten);
}
public static void sort(double[] array) {
for (int i = array.length - 1; i < 0; i--) {
double currentMax = array[i];
int currentMaxIndex = i;
for (int x = i - 1; x < -1; x--) {
if (currentMax < array[x]) {
currentMax = array[x];
currentMaxIndex = x;
}
}
if (currentMaxIndex != i) {
array[currentMaxIndex] = array[i];
array[i] = currentMax;
}
}
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
}
}
I believe your problem is here:
for(int i=array.length-1; i<0; i--)
array.length is not less than 0 so the for loop never runs. You probably wanted
for(int i=array.length-1; i>=0; i--)
Be Simple!
public static void selectionSort(double[] arr) {
for (int i = 0; i + 1 < arr.length; i++) {
int minIndex = findMinIndex(arr, i + 1);
if (Double.compare(arr[i], arr[minIndex]) > 0)
swap(arr, i, minIndex);
}
}
private static int findMinIndex(double[] arr, int i) {
int minIndex = i;
for (; i < arr.length; i++)
if (Double.compare(arr[i], arr[minIndex]) < 0)
minIndex = i;
return minIndex;
}
private static void swap(double[] arr, int i, int j) {
double tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

java - threads (returning an element from an array)

can someone tell me why the thread returns 0 after running this code, and not the max value from the array (the array must be as large as possible), how can I fix it?
import java.util.Arrays;
public class MyThread extends Thread {
int[] arr = new int[1000000];
public static int max(int[] numbers) {
int maxValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue) {
maxValue = numbers[i];
}
}
return maxValue;
}
public static int min(int[] numbers) {
int minValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue) {
minValue = numbers[i];
}
}
return minValue;
}
#Override
public void run() {
int maxArr = max(Arrays.stream(arr).toArray());
System.out.println(maxArr);
}
public static void main(String[] args) {
MyThread Thread1 = new MyThread();
Thread1.start();
}
}
Your array cells are not initialized, so all value are zero. Write 666 to arr[0] and have a nice day.
Your array
int[] arr = new int[1000000];
is array that filled with zeros, coz the default value of int is 0.
You should assign some values to array elements otherwise default values 0 are used.
You are passing an array arr which has only zero for all the index which means you need to initialise the array with values like below.
import java.util.Arrays;
class MyThread extends Thread {
int[] arr = new int[1000000];
public static int max(int[] numbers) {
int maxValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > maxValue) {
maxValue = numbers[i];
}
}
return maxValue;
}
public static int min(int[] numbers) {
int minValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue) {
minValue = numbers[i];
}
}
return minValue;
}
#Override
public void run() {
//initialise array with values
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
int maxArr = max(Arrays.stream(arr).toArray());
System.out.println(maxArr);
}
public static void main(String[] args) {
MyThread Thread1 = new MyThread();
Thread1.start();
}
}

Method returning null pointer error

There is the error I am getting
Exception in thread "main" java.lang.NullPointerException
at StudentGrades.getMinimum(StudentGrades.java:54)
at StudentClient.main(StudentClient.java:14)
I did not find any method that i set a null.
I tried using median method but still gets me this same error.
import java.util.Arrays;
import java.util.Random;
public class StudentGrades {
Random randomNumber = new Random();
int numberofStudents;
int grade;
int[] grades;
int sum = 0;
public StudentGrades(int studentNumber) {
numberofStudents = studentNumber;
int[] grades = new int[numberofStudents];
for (int i = 0; i < numberofStudents; i++) {
grades[i] = randomNumber.nextInt(101);
Arrays.sort(grades);
}
}
public int getNumberStudents() {
return numberofStudents;
}
public int[] getStudentGrades() {
int[] temp = new int[grades.length];
for (int i = 0; i < grades.length; i++) {
temp[i] = grades[i];
}
return temp;
}
public void setStudentGrades(int n) {
grade = n;
}
public double getAverage() {
for (int i = 0; i < grades.length; i++) {
sum = +grades[i];
}
double average = (double) sum / numberofStudents;
return average;
}
public int getMaximum() {
int max = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] > grades[max])
max = grades[i];
}
return max;
}
public int getMinimum() {
int min = grades[0];
for (int i = 0; i < grades.length; i++) {
if (grades[i] < grades[min])
min = grades[i];
}
return min;
}
public String toString() {
String returnString = "grades :";
for (int i = 0; i < grades.length; i++) {
returnString += grades[i];
}
return returnString;
}
public double getMedian() {
double median = 0;
if (grades.length % 2 == 0) {
median = (grades[grades.length / 2] + grades[(grades.length / 2) + 1]) / 2;
} else
median = grades[((grades.length - 1) / 2) + 1];
return median;
}
}
In your constructor you declared a local variable grades which did not initialize your instance variable grades:
int [] grades = new int [numberofStudents];
So the instance variable grades remains null. Try this:
grades = new int [numberofStudents];
which refers to the instance variable instead of declaring a local variable.

How to find the Odd and Even numbers in an Array?

Right now, I'm trying to find the odd and even numbers of an array. Here's the code of what I have so far. I know my findEvens() and findOdds() methods are messed up because they keep giving me off values whenever I try to print the final result. For example, if I try to find the odds of {1,5,8,3,10}, it gives me {5,3,0}. And if I try to find the evens of {2,5,8,7,19}, it gives me {2,8,0}. Anyone know why?
public class Scores {
private int[] numbers;
public Scores(int[] numbersIn) {
numbers = numbersIn;
}
public int[] findEvens() {
int numberEvens = 0;
for (int i = 0; i < numbers.length; i++) {
if (i % 2 == 0) {
numberEvens++;
}
}
int[] evens = new int[numberEvens];
int count = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
evens[count] = numbers[i];
count++;
}
}
return evens;
}
public int[] findOdds() {
int numberOdds = 0;
for (int i = 0; i < numbers.length; i++) {
if (i % 2 == 0) {
numberOdds++;
}
}
int[] odds = new int[numberOdds];
int count = 0;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] % 2 == 1) {
odds[count] = numbers[i];
count++;
}
}
return odds;
}
public double calculateAverage() {
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return (double) sum / numbers.length;
}
public String toString() {
String result = "";
for (int i = 0; i < numbers.length; i++) {
result += numbers[i] + "\t";
}
return result;
}
public String toStringInReverse() {
String result = "";
for (int i = numbers.length - 1; i >= 0; i--) {
result += numbers[i] + "\t";
}
return result;
}
}
You're problem is in counting how many even numbers you have
public int[] findEvens() {
int numberEvens = 0;
for (int i = 0; i < numbers.length; i++) {
if (i % 2 == 0) {
numberEvens++;
}
}
this will always return a number that is half the size of the length of numbers because you're doing mod division on the number of elements in the array, not on the elements themselves. Add numbers[i] to the if statement
public int[] findEvens() {
int numberEvens = 0;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] % 2 == 0) {
numberEvens++;
}
}
looks like you've got the same problem with odd count
'i' is used as conditional variable for looping. You should not divide this by 2. You have to divide the array element. like
if (numbers[i] % 2 == 0) {
numberEvens++;
}
then it should work. Thanks
Try This Code
import java.util.Scanner;
public class OddEven {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter number Elements in Array");
int n = s.nextInt();
int arr[] = new int[n];
System.out.println("enter Elements ");
for(int i=0; i<n; i++) {
arr[i]=s.nextInt();
}
int [] odd = filterOdd(arr);
try {
for(int i=0; i<n; i++) {
System.out.println("Odd" + odd[i]);
}
} catch(ArrayIndexOutOfBoundsException e) {}
int [] even = filterEven(arr);
try {
for(int i=0; i<n; i++) {
System.out.println("Even" + even[i]);
}
} catch(ArrayIndexOutOfBoundsException e) {}
}
public static int[] filterOdd(int[] a) {
int l = 0;
int j = 0;
for(int i=0; i<a.length; i++) {
if(a[i]%2==1) {
l++;
}
}
int k[]=new int[l];
for(int i=0; i<a.length; i++) {
if(a[i]%2==1) {
k[j] = a[i];
j++;
}
}
return k;
}
public static int[] filterEven(int[] a) {
int l = 0;
int j = 0;
for(int i=0; i<a.length; i++) {
if(a[i]%2==0) {
l++;
}
}
int k[] = new int[l];
for(int i=0; i<a.length; i++) {
if(a[i]%2==0) {
k[j] = a[i];
j++;
}
}
return k;
}
}
public class Array {
public static void main(String[] args) {
// TODO code application logic here
//Array declaration and value asign
int number[]=new int[]{1,2,3,4,5,6,7,8,9};
// for loop to move number
for(int p=0;p<number.length;p++)
{
// check number is even or odd??
if(number[p]%2==0)
System.out.println(number[p]+ " is Even number");
else
System.out.println( number[p]+" is odd umber");
}
}
}
Odd array one columns and another columns even array
public class OddEven {
public static void main(String[] args) {
int arr[]={1,2,3,4,5,6,7,8};
int ss[]=new int[10];
int odd[]=new int[10];
int i;
int k;
for( i=0;i<arr.length;i++)
{
if(arr[i]%2==0)
{
ss[i]=arr[i];
System.out.print(""+ss[i]);
System.out.print(" ");
}
if((arr[i]%2)!=0)
{
odd[i]=arr[i];
System.out.print(""+odd[i]);
System.out.print(" ");
}else
{
System.out.println(" ");
}
}
}
}
========================================output==============================
1 2
3 4
5 6
7 8

Categories