Results 1 to 7 of 7
  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Java Test question review help.

    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

    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]);
    		}
    	}
    }
    2 Errors.

    Q1.java:12: incompatible types
    found: float[]
    required: float
    return y;

    Q1.java17: incopatible types
    found: float
    required: float[]
    float[]y = doubleSize(x);


    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);
    	}
    }
    1 error, although im sure it is keeping other errors from popping up, having a lot of trouble figuring this one out.

    Q2.java23: ';' expected
    boolean contrainsPair()

  2. #2
    Nikkei's Hoe
    Worse than her at uno

    Join Date
    Dec 2006
    Posts
    6,236
    BG Level
    8
    FFXIV Character
    Eanae Hikari
    FFXIV Server
    Gilgamesh
    FFXI Server
    Cerberus
    WoW Realm
    Hyjal

    I'm not experienced in java at all, but if I had to take a guess, for error 1, you're trying to return a float array in a non-array defined method. For error 2, you didn't initialize y. Also you're trying to store a float into a float array. I'm most likely way off, but meh. =/

  3. #3
    Nikkei's Hoe
    Worse than her at uno

    Join Date
    Dec 2006
    Posts
    6,236
    BG Level
    8
    FFXIV Character
    Eanae Hikari
    FFXIV Server
    Gilgamesh
    FFXI Server
    Cerberus
    WoW Realm
    Hyjal

    For number two you need to declare a boolean variable. You can't just straight away call the method with nothing to store the result the method returns into.

    Code:
    boolean trueorfalse = containsPair(double[] x, double[] y);

  4. #4
    Cerberus
    Join Date
    Sep 2006
    Posts
    433
    BG Level
    4
    FFXIV Character
    Windred Sayna
    FFXIV Server
    Hyperion
    FFXI Server
    Phoenix

    Error #1: You want to return the entire array but your function is setup to return a float. You want to return a float[].
    Error #2: Fixing #1 should fix this.
    Error #3: When calling a function and passing parameters, you don't need to specify the datatype of the objects you are trying to pass in. (Unless you are declaring new ones at the same time, then you would need to use the new keyword.)

  5. #5
    Fake Numbers
    Join Date
    Jul 2008
    Posts
    86
    BG Level
    2

    Also on question 1, shouldn't the loop be for i<x.length? Not that it has anything to do with the error, it just seems like it would never enter the loop because i starts at 0 and then only loops if i is greater than x.length, which it never will be since it's 0.

  6. #6
    New Merits
    Join Date
    Jun 2008
    Posts
    219
    BG Level
    4
    FFXI Server
    Caitsith

    hey, i'm currently pursuing a B.S. in computer science at my college, and this stuff is oddly fun to me so i'll help!

    error 1 and 2) like stated above, in the method declaration for doubleSize, you are returning a type float. However, in your main you are setting a float array called y to be the result of doubleSize, which is a float, not a float[]. That is why you re getting the error. This can easily be fixed by changing the line "public static float doubleSize(float[] x)" to "public static float[] doubleSize(float[] x)".

    error 3) errors spit out by the interpreter are not always 100% accurate. For this one, it appears like you have types wrong. in your containsPair method, you should be returning a boolean, not a double. Booleans are true and false, which is what it appears to be what you want. This could be fixed by changing the line "public static double containsPair(double[] x, double[] y)" with "public static boolean containsPair(double[] x, double[] y)". Also, you need to declare a variable to be the result of this method, which is what it appears you tried to do in your main. So, instead of "boolean containsPair(double[] x, double[] y)" , what I believe you want is "boolean result = containsPair(x, y)". In java, once you declare a variable with a type, you no longer need to refer to it with that type name. You don't need to refer to x as double[] x, as you can just say x or y and it will point to the correct thing. Also, if you want to check the result to see if it works properly, you may want to throw in a print line like "System.out.println(result);"

    On another note, you may want to throw a checker in for your second problem that checks if the two arrays of doubles are the same length or not. Because if they aren't you are bound to get an index out of bounds error. (if this is an intro class they may not care, but from standard programming conventions you should be accounting for those cases).

  7. #7
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    ty so much guys this is helping a lot. As for catching the acceptation I dont have to in this particular case. He only wants method, not full code. I'm just doing full code to test it out.

Similar Threads

  1. Blogging question/suggestions/help
    By Rulke in forum Tech
    Replies: 5
    Last Post: 2011-02-10, 14:34
  2. Java code question
    By Skie in forum Tech
    Replies: 4
    Last Post: 2011-01-13, 23:33
  3. SLI question please help
    By Arximiro in forum Tech
    Replies: 13
    Last Post: 2010-08-11, 00:32
  4. Auto-saved password question, please help
    By Belladonna in forum Tech
    Replies: 8
    Last Post: 2009-02-05, 21:51