SRL Archive - Java Help and Tutorials

First try at Java


Element17

2013-08-27 18:24:26

Hello guys! I am taking a Java class online at the University I attend and this is my first assignment!

Exercise
Write a Java program to print the following information on
separate lines.
1. Your name.
2. The Department Name, Course Number, and Time for each course that you are taking this semester. Put the information for each course on a separate line.
3. Skip two lines (see notes for explanation).
4. The email address that you regularly use.

Notes and Comments

The file MUST be named as LastnamePass1.java (for example, my last name is Li, it will be named as LiPass1.java). Be sure to compile your program, correct any syntax errors, recompile until no errors, then test the program with different values. Upload only the .java file to D2L dropbox folder named Programming Assignment 1.

Note 1:
Use System.out.println( ) without any arguments to generate a new line character (i.e.,skip a line).
Note 2:
In Java, the class name and the file name must match exactly(case-sensitive).
If the file name is LastnamePass1.java, the Java class must also be LastnamePass1. Otherwise it will fail the compiling and you will lose 20 points for this.
Note 3:
Please pay close attention to the indentation and spacing. Read the code in the textbook to see how you can well format the source code.





This is my first try at it….ehhh? Better way to do it?


public class Assignment1 {

public static void main(String[] args){
System.out.println("Andrew Varnick");
System.out.println("Physics, 1930 5:10-7:20");
System.out.println("Chemistry, 2394 2:20-5:20");
System.out.println("Computer Science, 1015-W1");
System.out.println("Communications, 1200-WA2");
System.out.println();
System.out.println();

}

}

euphemism

2013-08-27 21:02:00

You could use new line characters and have a single System.out.println() call instead of multiple.

Sin

2013-08-27 21:05:02

Missing your email. (Or you left it out on purpose)

Also, you can do:
 code 
for us to see better.

Why not make an array of strings and have the program loop through it while printing? It'll give your teacher a good impression.

NKN

2013-08-28 07:08:18

From: Sin
Missing your email. (Or you left it out on purpose)

Also, you can do:
 code 
for us to see better.

Why not make an array of strings and have the program loop through it while printing? It'll give your teacher a good impression.

Because I'm assuming he's never done Java and he doesn't want to get in over his head just yet. :s

core

2013-08-28 09:16:47

From: Element17

public class Assignment1 {

public static void main(String[] args){

System.out.println("Andrew Varnick");
System.out.println("Physics, 1930 5:10-7:20");
System.out.println("Chemistry, 2394 2:20-5:20");
System.out.println("Computer Science, 1015-W1");
System.out.println("Communications, 1200-WA2");
System.out.println();
System.out.println();

}

}


Looks fine to me.

From: NKN
Because I'm assuming he's never done Java and he doesn't want to get in over his head just yet. :s


I agree with NKN. What Element17 has meets the requirements well enough (barring name and missing email), and seems to do exactly what the notes imply.

There's no reason to complicate anything. Having multiply System.out.println statements makes the presentation of the output clear and straight forward, no need to mess about with new line characters or arrays.

In fact, this might result in point deduction. If you want to deviate at all from what you already have, you should consult your instructor prior to implementing those changes.

If this were anything more advanced, I'd probably suggest using a String formatter and aligning all of the class information to consistent columns. You might be able to do this manually in this case as well to get a better looking output.

J J

2013-08-28 09:57:01

From: euphemism
You could use new line characters and have a single System.out.println() call instead of multiple.

Would only put new line characters on the last line to keep the readability.

public class Assignment1 {

public static void main(String[] args){
System.out.println("Andrew Varnick");
System.out.println("Physics, 1930 5:10-7:20");
System.out.println("Chemistry, 2394 2:20-5:20");
System.out.println("Computer Science, 1015-W1");
System.out.println("Communications, 1200-WA2\n\n");

}

}

Also for note 3 you can use System.out.printf http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html But it might be too advanced for this assignment already. I think manually formatting would be fine aswell (using \t for tab or just spaces)

Solar

2013-08-28 10:13:09

Can you not just use a different print command line for printing on a new line to avoid needing at least one of the println()'s?

Element17

2013-08-29 17:50:41

I would first like to thank everyone that added to this discussion! I did leave my e-mail out on purpose and I would also like to point out that class has only been going for 3 days lol. I wouldn't want to go to over the top on the first assignment ha, but I will probably continue to post my assignments along with MY work. I wont really be asking for help unless I don't understand something and have talked to my professor. I will just post just giving my code and ask for better ways or if I have done something wrong! It is always nice to know before you turn something in! Again thanks guys!