Why doesn't this class execute properly? - java

Here is my first class called World
public class World {
private static char[][] world2D;
private int characterRow;
private int characterColumn;
public World(int width, int height){
world2D = new char[width][height];
characterColumn = 0;
characterRow = 0;
for(int i = 0; i < world2D.length; i++){
for(int j = 0; j < world2D[i].length; j++){
world2D[i][j] = '-';
}
}
world2D[characterRow][characterColumn] = 'P';
}
public void moveUp(){
world2D[characterRow][characterColumn] = '-';
if (characterRow > 0){
characterRow -= 1;
}
world2D[characterRow][characterColumn] = 'P';
}
public void moveDown(){
world2D[characterRow][characterColumn] = '-';
if (characterRow < world2D.length){
characterRow += 1;
}
world2D[characterRow][characterColumn] = 'P';
}
public void moveRight(){
world2D[characterRow][characterColumn] = '-';
if (characterColumn < (world2D[characterRow].length - 1)){
characterColumn += 1;
}
world2D[characterRow][characterColumn] = 'P';
}
public void moveLeft(){
world2D[characterRow][characterColumn] = '-';
if (characterColumn > 0){
characterColumn -= 1;
}
world2D[characterRow][characterColumn] = 'P';
}
public static void displayWorld(){
for(int i = 0; i < world2D.length; i++){
for(int j = 0; j < world2D[i].length; j++){
System.out.print(world2D[i][j]);
}
System.out.println();
}
}
}
Here is my second class called Driver
import java.util.Scanner;
public class Driver {
public static void main(String[]args){
#SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("How tall should the world be?: ");
int height = input.nextInt();
System.out.print("How wide should the world be?: ");
int width = input.nextInt();
World myWorld = new World(width,height);
World.displayWorld();
}
}
Why don't I need to call displayWorld specifically on the myWorld instance of the World class?
What if I created multiple World instances? This can't be right.
**edit for more detail
I want to call one of the class methods (i.e. moveUp or moveDown) on the instance of the World class myWorld object. However, I can't pass my reference to that object (myWorld) into those methods. I want to be able to call one of those methods, which changes the postion of 'P' in the 2 dimensional array and print it out using the methods that I have defined including the displayWorld method

Please refer to this link to learn more about static types.
If you want to display worlds of different instances, remove static from public static void displayWorld() and call myWorld.displayWorld().

Static variables, such as private static char[][] world2D, exist within a class, not objects of that class. This means they are accessed through the class name, not an instance name. Since you are initializing it in your constructor, it will be changed each time you create a new world, but the value will be the same for each instance. It looks like you should make the variable world2D non-static (just remove the static keyword) and do the same on the function displayWorld()
Then, the line World.displayWorld(); can be replaced with myWorld.displayWorld();

Related

Getting syntax error insert Class body to complete class declaration for the class "convert to binary"

class decimaltobinary{
int y= 56;
int array[] = new int[10];
public static void main(String[] args) {
converttobinary con = new converttobinary();
}
class converttobinary{ //Error occurring at this line
for(int i = 0; i<11;i++) {
while(y > 0) {
int x = y%2;
array[i]= x;
}
}
}
}
// Error occurring at the class convert to binary.
// It says insert Class body to complete class declaration.
Well the error speaks for itself.
Insert Class body to complete class declaration.
For loops must be either inside a method or a block.
class converttobinary {
{
for (int i = 0; i < 11; i++) {
while (y > 0) {
int x = y % 2;
array[i] = x;
}
}
}
}
And then you'll have another problem in your code.
Non-static variables cannot be referenced from a static context.
When you are coding in the main method note that it is static (public static void main(...)) so either you have to make all variables static or create a new instance of decimaltobinary class.
Easiest way to solve this is by creating a new instance of decimaltobinary
converttobinary con = new decimaltobinary().new converttobinary();
Below code must be kept inside some method :
for(int i = 0; i<11;i++) {
while(y > 0) {
int x = y%2;
array[i]= x;
}
}

Object is null after being initialized in a method

I'm new to programming and I have a problem in my code.
I start by initializing the Maze object to null because I don't know which will be the choice of the user. It is initialized in the method createMaze() but when I call printMaze it is null.
import java.util.Scanner;
public class Main {
public static final String EXITGAME = "EXIT";
public static final String PRINTBOARD = "PRINT";
public static final String CREATEMAZE = "UPLOAD";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Maze maze = null;
menu(maze,in);
}
private static void menu(Maze maze, Scanner in){
String option = in.nextLine();
while(option.toUpperCase() != EXITGAME){
switch(option.toUpperCase()){
case PRINTBOARD: printMaze(maze); break;
case CREATEMAZE: createMaze(in,maze); break;
}
System.out.println();
option = in.nextLine();
}
}
private static void printMaze(Maze maze){
if(maze != null){
int maxX = maze.getxSize();
int maxY = maze.getySize();
for(int x = 0; x < maxX; x++){
for(int y = 0; y < maxY; y++){
System.out.print(maze.getMazeRepresentation(x, y));
}
System.out.println();
}
}
else
System.out.println("Maze is undefined");
}
private static void createMaze(Scanner in, Maze maze){
if(maze == null || !maze.getGameStatus()){
int x = in.nextInt();
int y = in.nextInt();
in.nextLine();
char rawMaze[][] = new char[x][y];
maze = new MazeClass(x,y);
for(int i = 0; i < x; i++){
createMazeLine(in,i,y, rawMaze);
}
maze.createMaze(rawMaze);
}
else
System.out.println("Maze already defined.");
}
private static void createMazeLine(Scanner in, int lines, int y, char[][] rawMaze){
String line = in.nextLine();
for(int i = 0; i < y; i++){
rawMaze[lines][i] = line.charAt(i);
}
}
}
in Java when you pass parameter to the method (i.e. Maze maze createMaze(Scanner in, Maze maze) it is a value of reference to the maze.
In other words in your case:
there is a null value.
there is a reference to that null called null
in call to the method value of that reference passed (let say address) as pramemter
in method call there is a parameter variable which holds value of that reference
Java does not have "parameters by reference" or pointers conception.
So, when inside the method createMaze you do maze = new MazeClass(x,y);
you assign new object maze reference to the reference parameter variable.
But your initial null value and reference to it outside of method stay untouched and object you created inside method destroyed after method done.
In short, all you need is:
private static void menu(Maze maze, Scanner in){
String option = in.nextLine();
while(option.toUpperCase() != EXITGAME){
switch(option.toUpperCase()){
case PRINTBOARD: printMaze(maze); break;
case CREATEMAZE: maze = createMaze(in,maze); break;
}
System.out.println();
option = in.nextLine();
}
}
private static Maze createMaze(Scanner in, Maze maze){
if(maze == null || !maze.getGameStatus()){
... yuor code ...
maze.createMaze(rawMaze);
}
else
System.out.println("Maze already defined.");
return maze;
}
Of course code can be written in more clear style, but main point is
new value assignment to the method parameter inside the method does not replace value of variable passed as parameter in the caller

Why is my class variable rewriting itself after an unrelated method runs?

So I'm writing a basic MasterMind game that is... mostly functional. However, its exhibiting odd behavior and I'm unsure why.
The idea is that what defines a Code and its behavior is one file, the gameplay is another, and the Main just creates a new game and starts playing. When I initialize the game, the computer creates a new random string of 4 (the "secret code"), as expected; but then once I get input for the User guess, it seems to rewrite the secret code into whatever I've input. Further, my methods for evaluating matches don't work at all, but considering that the secret code keeps changing means that it's not being set to begin with, and I'm unsure why.
All three classes below. Why is my class variable in Game not setting properly and accessible to the other methods?
Main.java
class Main {
public static void main(String[] args) {
Game newGame = new Game();
newGame.play();
}
}
Code.java
import java.util.Random;
import java.util.HashMap;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Set;
import java.lang.Math;
import java.lang.StringBuilder;
class Code {
private static HashMap<String,String> PEGS;
private static ArrayList<String> pegStrings;
protected static String secretCodeString;
public static void main(String[] args) {
}
public Code(String input){
this.secretCodeString = input;
}
public Code(){
randomize();
}
//literally just creates the peghash
public static void setPegs(){
PEGS = new HashMap<String,String>();
PEGS.put("C","c");
PEGS.put("Y","y");
PEGS.put("R","r");
PEGS.put("P","p");
PEGS.put("O","o");
PEGS.put("G","g");
}
//turns the pegs ito something randomize can use
public static ArrayList<String> makePegArray(){
setPegs();
pegStrings = new ArrayList<String>();
Collection<String> pegValues = PEGS.values();
Object[] pegObjects = pegValues.toArray();
for (int i = 0; i < pegObjects.length; i++){
pegStrings.add(pegObjects[i].toString());
}
return pegStrings;
}
// sets Class Variable secretCode to a four letter combination
public static Code randomize(){
secretCodeString = new String();
Random rand = new Random();
int randIndex = rand.nextInt(makePegArray().size());
for (int i = 0; i < 4; i++){
randIndex = rand.nextInt(makePegArray().size());
secretCodeString = secretCodeString.concat(makePegArray().get(randIndex));
}
Code secretCode = parse(secretCodeString);
return secretCode;
}
public static Code parse(String input) {
setPegs();
makePegArray();
String[] letters = input.split("");
StringBuilder sb = new StringBuilder();
for (String letter : letters) {
if (pegStrings.contains(letter)) {
sb.append(letter);
} else {
System.out.println(letter);
throw new RuntimeException();
}
}
String pegListString = sb.toString();
Code parsedCode = new Code(pegListString);
//System.out.println(parsedCode);
return parsedCode;
}
public int countExactMatches(Code guess){
String guessString = guess.secretCodeString;
int exactMatches = 0;
String[] guessArray = guessString.split("");
String[] winningCodeArray = (this.secretCodeString).split("");
for(int i = 0; i < 4; i++){
if(guessArray[i] == winningCodeArray[i]){
exactMatches++;
}
}
return exactMatches;
}
public int countNearMatches(Code guess) {
String guessString= guess.secretCodeString;
HashMap<String,Integer> guessCount = new HashMap<String,Integer>();
HashMap<String,Integer> secretCodeCount = new HashMap<String,Integer>();
Set<String> codeKeys = guessCount.keySet();
int matches = 0;
int keys = guessCount.keySet().size();
String[] keyArray = new String[keys];
for(int i = 0; i < guessString.length(); i++) {
//removes character from string
String codeCharacter = String.valueOf(guessString.charAt(i));
String guessShort = guessString.replace(codeCharacter,"");
//counts instances of said character
int count = guessString.length() - guessShort.length();
guessCount.put(codeCharacter, count);
}
for(int i = 0; i < secretCodeString.length(); i++) {
//removes character from string
String winningString = this.secretCodeString;
String winningCodeCharacter = String.valueOf(winningString.charAt(i));
String winningCodeShort = guessString.replace(winningCodeCharacter,"");
//counts instances of said character
int count = winningString.length() - winningCodeShort.length();
secretCodeCount.put(winningCodeCharacter, count);
}
for (int i = 0; i < keys; i++) {
codeKeys.toArray(keyArray);
String keyString = keyArray[i];
if (secretCodeCount.containsKey(keyString)) {
matches += Math.min(secretCodeCount.get(keyString), guessCount.get(keyString));
}
}
int nearMatches = matches - countExactMatches(guess);
return nearMatches;
}
}
Game.java
import java.util.Scanner;
class Game {
protected static Code winningCode;
public static void main(String[] args){
}
public Game(){
winningCode = new Code();
}
protected static Code getGuess() {
Scanner userInput = new Scanner(System.in);
int count = 0;
int maxTries = 5;
while(true){
try {
String codeToParse = userInput.next();
Code guess = Code.parse(codeToParse);
return guess;
} catch(RuntimeException notACode) {
System.out.println("That's not a valid peg. You have " + (maxTries - count) + " tries left.");
if (++count == maxTries) throw notACode;
}
}
}
protected static void displayMatches(Code guess){
int nearMatches = winningCode.countNearMatches(guess);
int exactMatches = winningCode.countExactMatches(guess);
System.out.println("You have " + exactMatches + " exact matches and " + nearMatches + " near matches.");
}
protected static void play(){
int turnCount = 0;
int maxTurns = 10;
System.out.println("Greetings. Pick your code of four from Y,O,G,P,C,R.");
while(true){
Code guess = getGuess();
displayMatches(guess);
if (guess == winningCode) {
System.out.print("You win!!");
break;
} else if (++turnCount == maxTurns) {
System.out.print("You lose!!");
break;
}
}
}
}
On every guess, you call Code.parse, Code.parse creates a new Code (new Code(pegListString);) and that constructor sets the secretCodeString and because that's static, all instances of Code share the same variable. You need to avoid mutable static members.
Another tip is to either have a method return a value, or mutate state (of either its input, or its own instance, this), but avoid doing both.
"Why is my class variable rewriting itself after an unrelated method runs?"
Because, actually, it is not unrelated. The "mess" that you have created by declaring variables and methods as static has lead to unwanted coupling between different parts of your code.
It is difficult to say what the correct solution is here because your code has gotten so confused by the rewrites that it is hard to discern the original "design intent".
My advice would be to start again. You now should have a clearer idea of what functionality is required. What you need to do is to redo the object design so that each class has a clear purpose. (The Main and Game classes make sense, but Code seems to be a mashup of functionality and state that has no coherent purpose.)

Graph Matrix project (non-static in static)

I have an assignment to code a graph matrix that takes numbers labels them and spits out the connections. Right now have the basic problem of this non-static/static stuff. I don't understand the problem even though I thought I understood the difference between a class and an instance of that class. When i run this there is a problem at first for loop. Won't pause for input of the labels. I appreciate any help and/or criticisms.
public class GraphMatrix {
class Matrix {
private int matrix[][];
private int size;
private String labels[];
private void createMatrix() {
Scanner in = new Scanner(System.in);
System.out.println("How many points will be represented in this graph?");
size = in.nextInt();
matrix = new int[size][size];
labels = new String[size];
System.out.println("Please label each point.");
for (int i = 1; i <= size; i++) {
System.out.println("Enter label for point #" + (i));
String key = in.nextLine();
labels[i] = key;
}
System.out.println("Please define edges between points or enter -1 when finished:");
int finish = 1;
while (finish == 1) {
int jkey = 0;
int kkey = 0;
int count = 0;
boolean pass = false;
System.out.println("Point labeled:");
String j = in.nextLine();
while (pass = false) {
if (labels[count].equals(j)) {
jkey = count;
count = 0;
pass = true;
}
}
System.out.println("to point labeled:");
String k = in.nextLine();
while (pass = true) {
if (labels[count].equals(j)) {
kkey = count;
pass = false;
}
}
matrix[jkey][kkey] = 1;
System.out.println("Finished enter -1, to define more connections enter 1");
finish = in.nextInt();
}
}
private void listEdges() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (matrix[i][j] == 1) {
System.out.println("There is an edge between" + labels[i] + " and" + labels[j]);
}
}
}
}
}
public static void main(String[] args) {
Matrix neo = new Matrix();
neo.createMatrix();
neo.listEdges();
}
}
You need to make change in main method as follows to remove compiler error.
From
Matrix neo = new Matrix();
to
GraphMatrix graphMatrix = new GraphMatrix();
Matrix neo = graphMatrix.new Matrix();
Note: To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
I see one significant problem in your code.
This while (pass = false) { should be while (!pass) {
and while (pass = true) { should be while (pass) {
Also: Why are you nesting the Matrix class inside of the GraphMatrix class? That doesn't make sense to me. If I were you, I wouldn't do this unless it is an explicit requirement of your assignment.

How to run one Java class and then another Java class in Eclipse?

I know it may be weird question but I'm really stuck. I have simple program with two classes. I need pass array from class A to class B. I did it but I cannot test it because I have no idea how to run program. When I click on the run then only one class started. I wanted test whole program and cannot find anything how to do it. Is there any command or something which say run class A and then class B? Without it I cannot test class B because values from Array (class A) are not loaded :/ Hope you understand what I mean.
I'm using eclipse.
Thanks!
Class MarkCalculator
import java.util.Scanner;
public class MarkCalculator {
public static int[] exam_grade = new int[6];
public static int[] coursework_grade = new int[6];
public static int[] coursework_weight = new int[2];
public static int[] module_points = new int[6];
public static String module_grade, holder;
public static int counter1 = 0, counter2 = 0;
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
for (int i=0; i<3; i++){
System.out.printf(i+1+". Modelue"+" Enter grade of exam:");
while (!input.hasNextInt() ){
System.out.printf("Enter only numbers! Enter grade of your exam: ");
input.next();
}
exam_grade[i]=input.nextInt();
System.out.printf(i+1+". Modelue"+" Enter grade of coursework:");
while (!input.hasNextInt()){
System.out.printf("Enter only numbers! Enter grade of your coursework: ");
input.next();
}
coursework_grade[i]=input.nextInt();
}
computeMark(coursework_grade, exam_grade, module_points);
// calculate module grade
for(int i = 0 ;i < 3; i++){
if (module_points[i] < 35){
System.out.println(i+1+".Module: Fail");
}
else if (module_points[i] >= 35 && module_points[i] <= 40){
System.out.println(i+1+".Module: Pass by compensation");
counter1++;
}
else {
System.out.println(i+1+".Module: Pass");
counter2++;
}
}
holder = computeResult(module_points, counter1,counter2, module_grade);
System.out.println("Your stage result is: "+ holder);
input.close();
}
public static int[] computeMark (int coursework_grade[], int exam_grade[], int module_points[]){
coursework_weight[0]= 50;
coursework_weight[1]= 50;
for(int i=0;i<3;i++)
{
if (coursework_grade[i] < 35 || exam_grade[i] < 35){
module_points[i]=(coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100;
if (module_points[i] > 35){
module_points[i] = 35; }
else {
module_points[i] = 0;
}
}
else {
module_points[i]=((coursework_grade[i]*coursework_weight[0] + (exam_grade[i]*(100-coursework_weight[1])))/100); }
}
return module_points;
}
public static String computeResult (int module_points[], int counter1, int counter2, String module_grade ){
int sum = 0;
double average = 0;
for (int i = 0; i < 3; i++){
sum = sum + module_points[i];
average = sum / 3;
}
for (int i = 0; i < 3; i++){
if (counter2 == 3){
module_grade = "Pass";
}
else if (average >= 40 && counter1 <= 2) {
module_grade = "Pass by compensation";
}
else {
module_grade = "Fail";
}
}
return module_grade;
}
}
Class StudentChart
public class StudentChart {
public static void main(String[] args) {
for (int i = 0; i < 3; i++){
System.out.println(MarkCalculator.coursework_weight);
}
}
}
You only need one main method.
class A {
String s;
public A(String s){
this.s = s;
}
}
public class B {
public static void main(String[] args){
A a = new A("Hello");
System.out.println(a.s + " world!");
}
}
class B will be the application program, the one with the main method. It will get values from class A. class A does not need to run for class B app to work, even though it uses values from class A.
You can have a method with a different name in another class, and call that method from your main method.
Do not call it public static void main though - that should only be used for standalone programs. If the method requires some other code to be run prior to it, it should not be the main method of a Java program.

Categories

Resources