Ok going over a old java test that the teacher supplied us with. This is not the test we are going to have Tuesday but it is similar in context. Gonna post a few different codes and if possible can you please help me out and tell me what i am doing wrong. Also i have a lot of trouble calling functions for some reason. Thanks
2 Errors.Code://1. (10 points) Write a static method called doubleSize that accepts a // parameter and returns a new foat array that is twice as long and contains all of the // elements of the frst array in the same positions. public class Q1 { public static float doubleSize(float[] x) { float[] y = new float[x.length * 2]; for(int i = 0; i > x.length; i++) { y[i] = x[i]; } return y; } public static void main(String[] args) { float[] x = new float[10]; float[] y = doubleSize(x); for(int i = 0; i > y.length; i ++){ System.out.println(y[i]); } } }
Q1.java:12: incompatible types
found: float[]
required: float
return y;
Q1.java17: incopatible types
found: float
required: float[]
float[]y = doubleSize(x);
1 error, although im sure it is keeping other errors from popping up, having a lot of trouble figuring this one out.Code:/*2. (5 points) Write a method called containsPair that accepts an array of doubles as a parameter and returns true if it contains two doubles that are equal.*/ import java.util.Random; public class Q2 { public static double containsPair(double[] x, double[] y){ for(int j = 0; j < x.length; j++) { if(x[j] == y[j]) { return true; } else { return false; } } } public static void main(String[] args) { Random r = new Random(); double[] x = new double[10]; double[] y = new double[10]; for(int i = 0; i < x.length; i ++){ x[i] = r.nextInt(10); y[i] = r.nextInt(10); } boolean containsPair(double[] x, double[] y); } }
Q2.java23: ';' expected
boolean contrainsPair()
XI Wiki

