package gameClasses; public class Sprite { public static final int MAX_WIDTH = 6
ID: 3643364 • Letter: P
Question
package gameClasses;public class Sprite {
public static final int MAX_WIDTH = 64;
public static final int MAX_HEIGHT = 64;
public final int width, height;
private static int[] getDimensions(){ return {width,height}; }
}
import gameClasses.Sprite.*;
import java.lang.System.out;
class User {
public static void main(String[] args) {
new User().go();
}
void go() { out.println(Sprite.getDimensions()); }
}
/*
Q1: Why will the code above not compile? List the problems, then explain
thoroughly
Q2: Fix the errors in the code so that it will compile. Your source file should
be named c10q6.java.
*/
Explanation / Answer
1) width and height need to be initialized with values and changed from final to static variables. They should also be declared as private. The function getDimensions() must return an array (Currently the syntax is incorrect for doing that) and should be declared public. Both the import statements are incorrect: they need to be import gameClasses.*; and import java.lang.System;. A new user doesn't need to be created(the method go() can be called directly). The method go() also prints the output incorrectly and needs an accessor modifier. It also needs to call the method getDimensions using the double-colon syntax (Used to access static methods/variables) 2) package gameClasses; public class Sprite { public static final int MAX_WIDTH = 64; public static final int MAX_HEIGHT = 64; private static int width = 32; private static int height = 32; public static int[] getDimensions() { int[] x = new int[2]; x[0] = width; x[1] = height; return x; } } import gameClasses.*; import java.lang.System; class User { public static void main(String[] args) { go(); } public static void go() { int[] dim = Sprite::getDimensions(); out.println(dim[0]); out.println(dim[1]); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.