Next semester I have a programming course at school where we will be learning Java. Seeing as I'm more of a C++/Lua person, I thought I would teach myself some Java today and give myself a head start. I just wrote my first program, and want someone to tear apart my form now, so that I don't need to relearn Java after I'm halfway done a major project.
The program basically accepts three strings as input, then outputs them as if it were the cover as a book. The program works fine; I'm just interested in whether I'm violating any Java practices or making some sort of mistake that will come back to haunt me in the future.
Main Class:
[code]
package javaTesting;
public class Main {
public static void main(String[] args) {
System.out.println("Book Title:");
String bookTitle = GetInput.getString();
System.out.println("Book Author:");
String bookAuthor = GetInput.getString();
System.out.println("Book Content:");
String bookContent = GetInput.getString();
Book bookTest = new Book(bookTitle, bookAuthor, bookContent);
bookTest.showBook();
}
}
[/code]
Input Class:
[code]
package javaTesting;
import java.util.Scanner;
public class GetInput {
static Scanner inputStream = new Scanner(System.in).useDelimiter("\n");
public static String getString() {
String inputString = inputStream.nextLine().toString();
if(inputString.trim().equals("")) {
System.out.println("Error: Invalid input");
}
return inputString;
}
}
[/code]
Book Class:
[code]
package javaTesting;
public class Book {
private String bookTitle;
private String bookAuthor;
private String bookContent;
public Book(String title, String author, String content) {
setTitle(title);
setAuthor(author);
setContent(content);
}
private void setTitle(String newTitle) {
this.bookTitle = newTitle;
}
private void setAuthor(String newAuthor) {
this.bookAuthor = newAuthor;
}
private void setContent(String newContent) {
this.bookContent = newContent;
}
public void showBook() {
System.out.println("_____________\n" + this.bookTitle + "\n By: " + this.bookAuthor + "\n" + this.bookContent + "\n_____________");
}
}
[/code]
If anyone actually takes the time to go through my program, thanks for the help!
It'd probably be better if you made the GetInput class static.
[code]
public static class GetInput {
}
[/code]
So you can do
[code]
String bookTitle = GetInput.getString();
[/code]
Well, you are creating your GetInput-class three times for no reason. Just make it a static function and make the Scanner a static member, so you don't have to initialize that every time you call the function.
Also, if a user presses enter without having typed in any input, would you really call that a fatal error?
And the last thing, Java usually calls the 'stringification'-function toString.
[QUOTE=PvtCupcakes;17660366]It'd probably be better if you made the GetInput class static.
[code]
public static class GetInput {
}
[/code][/QUOTE]
I don't think you can do that ô.o
[QUOTE=PvtCupcakes;17660366]It'd probably be better if you made the GetInput class static.
[code]
public static class GetInput {
}
[/code]
So you can do
[code]
String bookTitle = GetInput.getString();
[/code][/QUOTE]
From what I understand, you can't declare a top-level class static, unless that is IDE specific; at least Netbeans isn't allowing it.
[editline]03:57PM[/editline]
[QUOTE=ZeekyHBomb;17660377]Well, you are creating your GetInput-class three times for no reason. Just make it a static function and make the Scanner a static member, so you don't have to initialize that every time you call the function.
Also, if a user presses enter without having typed in any input, would you really call that a fatal error?
And the last thing, Java usually calls the 'stringification'-function toString.
[/QUOTE]
Thanks, that's the sort of thing I was looking for. I had a feeling I was doing something wrong with that aspect, but wasn't sure how it could be done better.
[QUOTE=Shanethe13;17660477]From what I understand, you can't declare a top-level class static, unless that is IDE specific; at least Netbeans isn't allowing it.
[editline]03:57PM[/editline]
Thanks, that's the sort of thing I was looking for. I had a feeling I was doing something wrong with that aspect, but wasn't sure how it could be done better.[/QUOTE]
Then just make getString() static.
I thought you could make classes static.
[QUOTE=PvtCupcakes;17660554]Then just make getString() static.
I thought you could make classes static.[/QUOTE]
Changed that seconds before you posted just now :P Thanks though.
*delete*
[QUOTE=PvtCupcakes;17660554]Then just make getString() static.
I thought you could make classes static.[/QUOTE]
You can make inner classes static, so you can use them like normal classes.
It seems like Java is taught more than it is actually used. Would I be better off learning it, or C++.
[QUOTE=laserpanda;17705384]It seems like Java is taught more than it is actually used. Would I be better off learning it, or C++.[/QUOTE]
Java is one of the (if not [i]the[/i]) most used language in the industry.
[QUOTE=laserpanda;17705384]It seems like Java is taught more than it is actually used. Would I be better off learning it, or C++.[/QUOTE]
Java is one of the most used language because it's cross platform. But, every language has it's downfalls.
[QUOTE=noctune9;17705465]Java is one of the (if not [i]the[/i]) most used language in the industry.[/QUOTE]
Depends on what industry you have in mind. Enterprise business software, sure. Games, not so much.
[QUOTE=Wyzard;17706502]Depends on what industry you have in mind. Enterprise business software, sure. Games, not so much.[/QUOTE]
I never understood what those Java application servers like JBoss or Glassfish are for. As a matter of fact, I don't even know what the difference between Java SE and Java EE is.
[QUOTE=PvtCupcakes;17707425]I never understood what those Java application servers like JBoss or Glassfish are for. As a matter of fact, I don't even know what the difference between Java SE and Java EE is.[/QUOTE]
Java SE is for normal desktop applications while Java EE is for using databases, serving websites and so on.
You don't really HAVE to put this, since you are using the variable in the same class that it was declared.
Depending on what you need, for instance if you just want the title of the book, you might want to get some accessor methods. (getTitle(), etc) However, you don't really have to put them if you don't need them.
You don't need showBook(), just name it toString(). Whenever you print out the object of the class
[code]
System.out.println(object)
[/code]
Java will execute the toString() method automatically.
[QUOTE=Kurodragon;17707796]You don't need showBook(), just name it toString(). Whenever you print out the object of the class
[code]
System.out.println(object)
[/code]
Java will execute the toString() method automatically.[/QUOTE]
Thanks, I didn't realize it worked that way; might come in handy later on.
Sorry, you need to Log In to post a reply to this thread.