Java code issue

Java code issue

-Hello, World!-

There were quite a few issues with the code that caused it to not compile. I fixed all of them (I think) and have written out what was wrong:

  • Public needs to be changed to public
  • The second Guitar constructor is missing all its parameters.
  • Enums need to be referenced by their name, you cant just type in their values.
  • Your curly braces were all over the place. There were way more closing than opening, so I deleted a bunch of them at the end and ones that were tossed into the middle of the class.
  • Methods must be within classes.
  • Enums cant be declared inside methods, only inside classes.
  • Variable declarations must have a space between the type and the name of the variable. Example: int_a; needs to be int a;
  • Your Duration enum has no constructor that accepts and int.
  • Getting a random value of an enum based on its index is done by doing Notes.values()[a];, not Notes[a];
  • No idea where the return Optional.empty(); line should be, so I deleted it.
  • Youre missing the import for the Random class.
  • Strings cant be on multiple lines.
  • .orElse() is not a method. Deleted this, unsure what it was supposed to do.
  • colorName() is not a method. I changed it to: (this.guitarColor == null ? none : this.guitarColor.name()).
  • The name of the variable in the toString() method is not the same as what you try to return. I changed them both to infoString.
  • You have a TON of typos, namely with things like Manufacturer being Manufacur and return being rturn.
  • When initializing a variable, you must have a space after new.
  • Your getter methods do not return the correct type.
  • The name of the variable numString is repeatedly referred to as numStrings. I added the s to the original variable declaration.

Below are a list of things you really SHOULD change, but you dont need to change.

  • All enum names should be in ALL CAPS.
  • Method names should be written using camelCase. Example: myVal would have a getter named getMyVal().
  • Have blank lines between methods, constructors, and types.
  • Have a space after the start of a comment. Example: // Comment Bad Example: //Comment
  • Have some sort of sorting pattern for enums. For notes, it should be alphabetical.
  • There is no need to have another enum for the duration of notes. This can easily be done in the note itself.
  • There is no need to do String.valueOf(someObject) for concatenation. It is better to do someObject.toString() and best to just leave it as someObject because the runtime will do that operation for you.
  • There should be a space before and after arithmetic operators like + and -.

I would highly recommend you read up on some basic Java tutorials and look at the Google Java Style Guide. It is what I use and what Im sure a lot of people use to determine how code should look.

With all the changes and with the proper use of style, your code looks like this:

import java.util.Random;

/*
* File: Guitar.java
* Author: Eppards,Michael
* Date: June 11, 2018
*/

class Guitar {
    private int numStrings;
    private double guitarLength;
    private Color guitarColor;
    private Manufacturer guitarManufacturer;

    enum Manufacturer {
        GIBSON,
        FENDER,
        IBANEZ;
    }

    enum Color {
        RED,
        BLUE,
        BLACK;
    }

    enum Notes {
        A(1),
        B(4),
        C(1),
        D(2),
        E(0.25),
        F(0.5),
        G(0.5);

        double duration;

        private Notes(double duration) {
            this.duration = duration;
        }
    }

    // Default Constructor
    private Guitar() {
        this.numStrings = 6;
        this.guitarLength = 28.2;
        this.guitarColor = Color.RED;
        this.guitarManufacturer = Manufacturer.GIBSON;
    }

    // Argument Constructor
    private Guitar(int numStrings, double guitarLength, Color guitarColor, Manufacturer guitarMaker) {
        this.numStrings = numStrings;
        this.guitarLength = guitarLength;
        this.guitarColor = guitarColor;
        this.guitarManufacturer = guitarMaker;
    }

    // Getters
    public int getNumStrings() {
        return numStrings;
    }

    public double getGuitarLength() {
        return guitarLength;
    }

    public Color getGuitarColor() {
        return guitarColor;
    }

    public Manufacturer getGuitarManufacturer() {
        return guitarManufacturer;
    }

    // playGuitar method for 16 randomly selected musical notes
    public String playGuitar() {
        String play = [;

        int a;
        int b;
        // initiate a random number
        Random rn = new Random();
        // for loop to select random notes
        for (int k = 0; k < 16; k++) {
            a = rn.nextInt(7);
            b = rn.nextInt(5);
            play = play + Notes.values()[a] + ( + Notes.values()[a].duration + );
            if (k != 15) play = play + ,;
        }
        play = play + ];

        return play;
    }

    // used to return the info in a string
    public String toString() {
        String infoString = (numStrings= + this.numStrings + , guitarLength= + this.guitarLength + ,guitarManufacturer= + this.guitarManufacturer + ,guitarColor= + (this.guitarColor == null ? none : this.guitarColor.name()) + );
        return infoString;
    }
}

Hope this helps! Friendly reminder that I made a LOT of changes to this code. You really should go look at the steps I took and make the changes yourself, otherwise you could get in trouble with your school for submitting work that is not your own.

Java code issue

Leave a Reply

Your email address will not be published. Required fields are marked *