Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Silly Code

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Auburn, Alabama
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Silly Code

    I have only been working with Java for about a month. This is a silly code I wrote this morning supposedly buying a Robot or Cyborg from USA Manufactories. What do you think? Please focus more on the code than anything else. Is this sufficient work from a beginner such as myself or should I creating more complex code.



    import java.util.Scanner;
     
    class Cyborg {
    	String name;
    	int age;
     
    	void speak(String voice2) {
    		System.out.println(voice2);
    	}
    }
     
    class Robot {
    	String name;
    	int age;
    	int height; // In terms of feet.
     
    	void speak(String voice) {
    		System.out.println(voice);
    	}
     
    	int grow() {
    		return height;
     
    	}
    }
     
    public class Manuf {
     
    	public static void main(String[] args) {
     
    		System.out.println("Welcome to USA Manufactories.");
    		System.out
    				.println("Here we have many different technologies, first up is Jack, the Robot.");
    		System.out.println("");
    		Robot Jack = new Robot();
    		Jack.name = "Jack";
    		Jack.age = 20;
    		Jack.height = 999;
     
    		Jack.speak("Hello, my name is Jack.");
    		System.out.print("I am now " + Jack.age + ". ");
     
    		Jack.grow();
    		System.out.print("I have grown " + Jack.height + " feet.");
     
    		System.out.println("");
    		System.out.println("");
    		System.out.println("");
     
    		System.out.println("Next we a have a cyborg named Him.");
     
    		Cyborg Him = new Cyborg();
    		Him.speak("Hello, my name is him. I am a cyborg.");
     
    		System.out.println("");
    		System.out.println("");
     
    		System.out
    				.println("Okay, now you must pick the Cyborg or robot. Which will it be?");
    		System.out.println("Press 1 for the Robot and 2 for the Cyborg.");
     
    		Scanner choice = new Scanner(System.in);
    		int picked = choice.nextInt();
     
    		System.out.println("Great! You picked choice " + picked + ".");
     
     
    		switch (picked) {
    		case 1:
    			System.out.println("How would you like to pay for your Robot?");
    			break;
     
    		case 2:
    			System.out.println("How would you like to pay for your Cyborg?");
    			break;
     
    		default:
    			System.out.println("Since you did not pick an option, it will be mailed to you.");
    			break;
    		}
     
    		System.out.println("Press 3 for Cash, 4 for Check, or 5 for the bill to be mailed to you.");
     
    		Scanner payment = new Scanner(System.in);
    		int paymentMethod = payment.nextInt();
     
    		switch (paymentMethod) {
    		case 3:
    			System.out.println("Thank you for using USA Manufactories, come again.");
    			break;
     
    		case 4:
    			System.out.println("Thank you for using USA Manufactories, come again.");
    			break;
     
    		case 5:
    			System.out.println("Thank you for using USA Manufactories, come again.  Your bill should come shortly.");
    			break;
     
    		default:
    			break;
    		}
     
    	}
     
    }

    Thanks!


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Silly Code

    Cool program that you made. I have skimmed through your code and have one suggestion.

    		int picked = choice.nextInt();

    I would recommend checking the value they entered because an entered value could 'break' your program. For example, you may expect me to enter an integer, but what if I accidentally enter a letter? It would cause the program to 'break' and the user would have to restart the program. This may not be the preferred way by professionals (I am a beginner), but I recommend running a loop around the input, doing choice.nextLine() to receive input, and attempting to parse the input. If it succeeds, allow them to continue with the program. If it fails, they remain in the loop until providing valid input.

    (Someone may have a better answer though)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Silly Code

    Get in the habit now of commenting your code before or while you're writing it.

    Limit code line lengths to a reasonable value. The industry standard used to be 80 columns, but this may seem too short to some with today's larger monitors. Even so, 80 is still a common max line length in forums.

    There's no benefit to naming a class "Manuf" instead of "Manufactories". Don't use shortened names and abbreviations that may not be so clear a few months or years down the road.

    I know you're still crawling, but now that you're using multiple classes, the main() method should only be a few lines long, just enough to call the constructor from another class to get the ball rolling.

    Even if this were a program with ALL code driven by a main() method, the code in the main() method should be separated into methods. There's entirely too much going on in the main() method. Most methods - even main() - should do one thing as described by its name, do it very well, and do nothing else.

    Breaking the main() into methods - among other things - would have allowed you more control over your switch() statements, transferring control to another method when needed rather than falling through to dialog that doesn't make sense. (default: "It will be mailed to you" followed by choosing a payment method.)

    By Java convention, class names are capitalized, variable and method names begin with lowercase letters and are camel cased thereafter. The Robot object name 'Jack' should be 'jack'.

    A class' instance variables and methods should be given an access modifier. Most encourage starting with 'private' for variables and 'protected' for methods, and only relaxing from those if absolutely necessary, as in:
    [highlight=java]class Cyborg {
    private String name;
    private int age;
    [/higjlight]

    In keeping with the above suggestion, an object's instance variables should not be accessed directly, as in:

    Jack.name = "Jack";

    Rather a setter (or "mutator") method should be written to modify an object's instance variables:

    jack.setName( "Jack" );

    This approach enforces the concept of 'encapsulation.' However, there are still design best practices to learn to minimize the 'dangers' that getters and setters pose. You can learn more by reading articles found by Googling, 'getters and setters evil'.

    System.out.println(""); is the same as System.out.println();, or you could simply add the needed number of "\n"s to the preceding print() statement, like:

    System.out.print("I have grown " + Jack.height + " feet.\n\n\n");

    or the following instead of using 3 println() statements:

    System.out.println( "\n\n" );

    A programmer strives to eliminate repeated code (wasted effort) whenever possible. Cases 3, 4, and 5 of your last switch() statement include repeated code that could be eliminated. (In fact, cases 3 and 4 could be combined, but I realize this is a work in progress for study purposes and may be left as is for future growth.)

    Good start! Keep coding.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    Votek (June 20th, 2014)

  5. #4
    Junior Member
    Join Date
    Jun 2014
    Location
    Auburn, Alabama
    Posts
    5
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Silly Code

    Thanks guys! I'll try and work on these things in the future.

Similar Threads

  1. Silly issue with java class
    By obadanasr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2013, 02:55 PM
  2. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  3. I am making a silly mistake but I cant find where! PLEASE HELP!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2012, 01:44 AM
  4. Silly error
    By American in forum Java Theory & Questions
    Replies: 5
    Last Post: August 23rd, 2010, 01:31 PM
  5. Im getting 1 Silly error can someone help please
    By JavaNoob82 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 24th, 2010, 06:55 PM