calling another method from different class - java

I was practicing java. And I created a method in another class within the same package such that,
class ArraysPractice2{
int n;
int[] arr = new int[n];
double averageA(int [] arr)
{
double ans = 0;
int added = 0;
int total = arr.length;
for(int i = 0; i < arr.length; i++)
{
added = added + arr[i];
}
ans = added / total;
return ans;
}
}
and then I have a main method has following code,
ArraysPractice2 aT = new ArraysPractice2();
int[] testArr = new int[10];
for(int i = 0; i < testArr.length; i++)
{
testArr[i] = i + 1;
}
aT.averageA(testArr);
I expected that aT.averageA would give me the average of testArr array.
However, it does not give me anything.
What are the issues in here?
I have a feeling that I am not understanding OOP well..
Thank you for your help.

aT.averageA(testArr);
You are not receiving/using a value you have got in the line above.
Try this:
double res = aT.averageA(testArr);
System.out.println("aT.averageA(testArr = "+res);
and you should see your value.

Related

null values keep appearing

I want to merge 2 Stirng arrays
the first one is merged okay but the second one keeps having null values even though it isn't empty
no errors just wrong values
what is the problem here?
public class Q4 {
public static void main(String[] args){
String array1[] = new String[]{"Ahmad", "Adam"};
String array2[] = new String[]{"Mick", "Ali"};
int n1 = array1.length;
int n2 = array2.length;
String []array3 = new String[n1+n2];
for(int i = 0; i < n1; i++)
array3[i] = array1[i];
for(int i = n1; i<n2; i++) {
int j = 0;
array3[i] = array2[j++];
}
for(int i = 0; i<array3.length; i++)
System.out.print(array3[i] + " ");
}
}
the output should be
Ahmad Adam Mick Ali
but this is what I get
Ahmad Adam null null
Your Second loop's i value is n1 size(2).and loop will continue before n2 size(2).that's why value not added.
Solution: U need add value after 1st array. If you will Store value from 1st array size ,then it will store value perfectly.
public class Q4 {
public static void main(String[] args){
String array1[] = new String[]{"Ahmad", "Adam"};
String array2[] = new String[]{"Mick", "Ali"};
int n1 = array1.length;
int n2 = array2.length;
String []array3 = new String[n1+n2];
for(int i = 0; i < n1; i++){
array3[i] = array1[i];
}
for(int i = 0; i<n2; i++) {
array3[n1++] = array2[i];
}
for(int i = 0; i<array3.length; i++)
System.out.print(array3[i] + " ");
}
}
for(int i = n1; i<n2; i++) {
the condition inside the second for loop is wrong.
As it is now you are starting from size of first array (2) to size of second array (2)
what you want should be
for(int i = n1; i<n2+n1; i++) {
also you are declaring int j inside the for loop which means j is reset each loop, move its definition outside of the loop or better yet replace it with
i-n1
In the second for loop I should've used i<array3.length instead of i<n2
since i=n1 and n2=n1 so for loop wasn't even excuted

Java modify array elemens

Hey guys I want to write program that shifts elements in an array once to the left but I tried everything and it's not working. The code below is outputting just 0's. Can someone please tell me how I can do this. Thank you!
import java.util.Scanner;
public class StudentScores {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
final int SCORES_SIZE = 4;
int[] oldScores = new int[SCORES_SIZE];
int[] newScores = new int[SCORES_SIZE];
int i;
for (i = 0; i < oldScores.length; ++i) {
oldScores[i] = scnr.nextInt();
}
int temp = newScores[0];
for (i = 0; i < oldScores.length - 1; ++i) {
newScores[i] = newScores[i+1];
}
temp = newScores[oldScores.length - 1];
for (i = 0; i < newScores.length; ++i) {
System.out.print(newScores[i] + " ");
}
System.out.println();
}
}````
As pointed out by people in comments, you seem to be assigning newScores[] to newScores[] value(an uninitialized array has all values set to 0):
int temp = newScores[0]; // Should be oldScores[0]
// ...
newScores[i] = newScores[i+1]; // Should be oldScores[i+1]
You need to have oldScores[] values on RHS. Since you only want to shift by one place, I think extra array is unnecessary. You can do it in place like this:
int temp = oldScores[0];
for (i = 0; i < oldScores.length - 1; ++i) {
oldScores[i] = oldScores[i + 1];
}
oldScores[oldScores.length - 1] = temp;

Temp integers not resetting

I am creating a histogram using another class to help display it, but that is not important to my question. Let me start by showing my code below
public class DisplayHistogram {
public static void main(String[] args) {
int temp = 0;
int holder = 0;
int average;
Random rand = new Random();
Histogram x = new Histogram(1, 20);
for(int i = 0; i<=10000; i++)
{
for(int j = 0; j<=3; j++)
{
temp = rand.nextInt(20) + 1;
holder = holder + temp;
}
average = Math.round(holder / 3);
x.submit(average);
}
System.out.println(x.toString());
}
}
the problem I am having is I set the histogram to have a min of 1 and max of 20. I am generating three random integers and finding the average of the 3 and submitting it to the histogram 10,000 times. However, after the first loop of 10000, the "holder" variable doesn't reset back to 0 causing my program trying to submit a value outside of the max, and creating an error. I have attempted to set holder to 0 at the end of every loop by doing
x.submit(average);
holder = 0;
temp = 0;
However that does not help.
I have tried some of your suggestions making my code look like
import java.util.*;
public class DisplayHistogram {
public static void main(String[] args) {
Random rand = new Random();
Histogram x = new Histogram(1, 20);
for(int i = 0; i<=10000; i++)
{
int temp = 0;
int holder = 0;
int average = 0;
for(int j = 0; j<=3; j++)
{
temp = rand.nextInt(20) + 1;
holder = holder + temp;
}
average = Math.round(holder / 3);
x.submit(average);
}
System.out.println(x.toString());
}
}
However it still returns this error
Exception in thread "main" HistogramOutOfBoundsException:
*******
Submitted value 22 is outside range [1,20] of Histogram.
*******
at Histogram.submit(Histogram.java:31)
at DisplayHistogram.main(DisplayHistogram.java:19)
Write your loops like this:
for(int i = 0; i<=10000; i++)
{
holder = 0; // add this line
for(int j = 0; j<=3; j++)
{
temp = rand.nextInt(20) + 1;
holder = holder + temp;
}
average = Math.round(holder / 3);
x.submit(average);
}
System.out.println(x.toString());
}
Fixed. Was looping four times instead of three. For loop should've looked like
for(int i=0; i<=2; i++)
Short answer
Pay attention to details.
The answer you want
Unlike the other answer,
don't just initialize the holder variable every loop.
Instead, minimize the scope of the holder variable to the loop.
public static void main(String[] args)
{
int average;
Random rand = new Random();
Histogram x = new Histogram(1, 20);
for (final int trialCount = 0; trialCount <= 10000; ++trialCount)
{
int holder = 0;
for (final int sampleCount = 0; sampleCount <= 3; ++sampleCount)
{
int temp = rand.nextInt(20) + 1;
holder += temp;
}
average = Math.round(holder / 3);
x.submit(average);
}
System.out.println(x.toString());
}

method for concatenating two different type arrays

this is my code, has no error while compile, but have run time error.
have exception ans do not show me output, where did i go wrong?
public static Object [] concatenate(int[]a ,double[] b) {
int l=a.length;
int L = b.length;
Object[] ob=new Object[l+ L];
for(int i=0;i<l;i++){
ob[i] = a[i];
}
i created the new object to add the two arrays into it.
for(int j=0;j<L;j++){
ob[j+l] = b[j];
}
And used two for for printing them.
System. out. print (ob);
return ob;
}
when you do this
System. out. print (ob);
you are actually printing the hash code of the Array object, and not its content..
something like this can make more sense:
System.out.println(Arrays.toString(ob));
So I tried your code and works fine
public static void main(String[] args) {
int[] intArr = new int[5];
for (int i = 0; i < intArr.length; i++) {
intArr[i] = 0;
}
double[] doubleArr = new double[5];
for (int i = 0; i < doubleArr.length; i++) {
doubleArr[i] = 1.11;
}
int l = intArr.length;
int L = doubleArr.length;
Object[] ob = new Object[l + L];
for (int i = 0; i < l; i++) {
ob[i] = intArr[i];
}
for (int i = 0; i < L; i++) {
ob[l + i] = doubleArr[i];
}
System.out.println(Arrays.toString(ob));
}
this prints
[0, 0, 0, 0, 0, 1.11, 1.11, 1.11, 1.11, 1.11]

Using two scanners.. one for integer, one for double which is causing my code not to work.

Question: How do I fix this? The code works when I feed the values, but when I use a scanner to read values it stops reading input after I enter the number of cities and throws a run time exception.
I tried to use two scanners instead of one.. One for double and one for integer and Im pretty sure thats not how you do it because then I see "lossy conversion from double to int". Can someone help? FYI, I am a UX architect and Im new to this. So any help will be greatly appreciated.
import java.util.*;
import java.lang.Math;
public class Centralcity {
public static void main(String[] args){
int numcities;
int x = 0;
int y = 0;
double shortest = 0;
double distance1 = 0;
double distance2 = 0;
int closestX1=0, closestX2=0, closestY1 =0 , closestY2 = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number of cities: " );
numcities = scanner.nextInt();
double[] xcor=new double[numcities];
double[] ycor=new double[numcities];
double[][] distance= new double[numcities][numcities];
double[] sumdistance= new double[numcities];
double[] temp= new double[numcities];
//numcities = 5;
//double xcor[]=new double[] {2.5,5.1,1,5.4,5.5};
//double ycor[]=new double[] {5,3,9,54,2.1};
for(int i = 0; i<numcities; i++)
{
for(int j = 0; j<numcities; j++)
{
System.out.println("Enter coordinates of the city:");
xcor[i]=scanner.nextDouble();
xcor[j]=scanner.nextDouble();
ycor[i]=scanner.nextDouble();
ycor[j]=scanner.nextDouble();
}
}
for(int i = 0; i<numcities; i++)
{
for(int j = 0; j<numcities; j++)
{
distance[i][j]=Math.pow((Math.pow(xcor[i]-xcor[j], 2)) + (Math.pow(ycor[i]-ycor[j], 2)), 0.5);
sumdistance[i]+=distance[i][j];
}
}
temp=sumdistance;
double temp1;
for(int i = 0; i < temp.length; i++)
{
for(int j = 1; j < (temp.length -i); j++)
{
if(temp[j-1] > temp[j])
{
temp1 = temp[j-1];
temp[j-1]=temp[j];
temp[j]=temp1;
}
}
}
double finl=0;
for(int i = 0; i <numcities; i++)
{
if(sumdistance[i]==temp[0])
{
finl=i;
}
}
System.out.println("The Central City coordinates are: " +xcor[finl]+","+ycor[finl]);
}
}
I assume you're telling us that the error you receive is "lossy conversion from double to int". You are not able to convert a double value to an int value. Either your input data or the way you read in the data should change.
A Q&A on why you can't convert double to int: Why can int/byte/short/long be converted to float/double without typecasting but vice-versa not possible
Change the variable finl to int type. like:
int finl=0;
for(int i = 0; i <numcities; i++)
{
if(sumdistance[i]==temp[0])
{
finl=i;
}
}
System.out.println("The Central City coordinates are: " +xcor[finl]+","+ycor[finl]);
It will work.

Categories

Resources