0

I'm completely new to programming, and I'm having difficulties understanding my assignment, and how I proceed solving it.

The assignment is introduced by saying: The String class defines a length accessor method with the following header: public int length() So the following is an example of its use with the String variable fullName; fullName.length()

The assignment then asks me to add conditional statements to the constructor of Student to print an error message if either the length of the fullName parameter is less than four characters or the length of the studentId parameter is less than three characters. However, the constructor should still use those parameters to set the name and id fields, even if the error message is printed.

The assignment also suggests that use if and else methods.

I've read up on everything until this point, but I simply don't understand how I proceed - I do, however, understand what it's supposed to function.

Can you guys perhaps lead me the way, or just give some hints on how I get started on this? It's noteworthy to mention that this is coded in BlueJ, and it's using an example of the project "LabClass".

TaZz
  • 652
  • 7
  • 20
JensJakob92
  • 5
  • 1
  • 4
  • 2
    Best way to start is to read the tutorial once again. Search over net about those methods and how to use it. That will help you to learn the syntax. – Helios Feb 06 '14 at 11:34
  • I'm really trying, trust me.. I've read it numerous times, and I just don't seem to understand it. I started programming (started at my school) 1½ week ago, and I just don't seem to be able to get the hang of it. – JensJakob92 Feb 06 '14 at 11:41
  • Please consider reading oracle docs. The best thing to learn java. For example: [String](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) – Helios Feb 06 '14 at 11:47
  • Please tell us what you have tried (aside from reading everything) to solve this assignment. – Alexander Tobias Bockstaller Feb 06 '14 at 12:04
  • @AlexanderTobiasHeinrich I must admit that I haven't tried that much yet, because I'm using the class example from the book, so I'm afraid that I'll ruin something, and just get stuck in something I don't understand. So at the moment I'm mainly trying to read and understand some of the replies I get to my problem. I'll hopefully get it very soon though. – JensJakob92 Feb 06 '14 at 12:09

2 Answers2

1

Let's first initialize object of your class Student leaving error checks aside. As you said, you are obliged to set these two fields (fullName and studentId) no matter what. So thats what we would have:

public class Student {

    private String studentId;
    private String fullName;

    public Student(String studentId, String fullName) {
        this.studentId = studentId;
        this.fullName = fullName;
    }

    public static void main(String[] args) {
        Student s = new Student("12", "John Doe");
    }
}

Above code does what you need in the first place. I hope you understand what is going on there? If not, then ask.

OK, what do we need now is adding this "length check". The key is to understand "if" construct. "If" allows you to execute some block of code only if certain condition is met. Condition may be any expression, but it must be evaluated to "true" or "false" (it needs to be boolean). "If" construct looks like this:

if (expression) {
    System.out.println("Expression was true!");
}

You can put in place of "expression" your condition and if happens it is true, the code will be executed. See this:

if (3 > 2) {
    System.out.println("Three is greater than two");
}

Well, we can use this knowledge to perform our length tests:

if (fullName.length() < 4) {
    System.err.println("Name '" + fullName + "' is too short");
}

Use the same construct (but different condition) to test studentId. I believe you should be able to modify the original source to check for string lengths.

As to this assignment --- you don't need else construct in this case, but to be complete: you can use it in combination with "if", to execute certain code, when the condition is not met, like this:

if (fullName.length() < 4) {
    System.err.println("Too short!");
}
else {
    System.out.println("OK");
}

Hope this helps.

Cromax
  • 1,822
  • 1
  • 23
  • 35
  • Thank you for your reply - it somehow helped me understand it a bit more. I do, however, have a few questions to your reply. In the constructor you've put in: public static void main(String[] args) { Student s = new Student("12", "John Doe"); which I've never seen before, so that confuses me greatly. Also, I might have misphrased the assignment a bit, it actually says that I shouldn't use "else", I think. It says: Hint: Use if statements of the following form (that is, having no else part) to print the error messages. Also; is boolean implemented in "if", or am I wrong? – JensJakob92 Feb 06 '14 at 11:55
  • As you examine it closely, you will see it's not placed in the constructor. Constructor starts with "public Student(String studentId, String fullName)" and this is called "declaration", as this just declares (tells "there's something like that") and after that you have constructor's body which starts with '{' and ends with '}' (it contains two lines in this case). Next, the "main" thing is a "method", which is invoked by java, when you start your program (BTW: constructor is a method too). – Cromax Feb 06 '14 at 11:59
  • It's just that my constructor almost looks identical to your "method". And I've been told that the constructor's header will always have the same name as the class, so I assume that only the constructor may have that name, hence my jump to the conclusion that you just wrote a constructor. – JensJakob92 Feb 06 '14 at 12:03
  • If "main" confuses you, you can ignore it for now (just delete it). The purpose of it was to demonstrate the use of your class (if you run it, you should see some output) --- but it's not relevant to solving your assignment. – Cromax Feb 06 '14 at 12:05
  • Yes, constructor has the name as its containing class, but technically it's a method. You can think (for now) of a method as of a "named piece of code" you can execute. For instance length() is also a method of String class --- you invoke it checking this string's length. But a class can contains many methods (many different constructors and many others "named pieces of code"). – Cromax Feb 06 '14 at 12:09
  • @user3279328 Student s=new Student("12", "John Doe"); instantiates an object of class Student with studentId 12 and fullName John Doe. – Wajahat Feb 06 '14 at 12:12
  • @Wajahat Thank you for trying to clarify the meaning of it, but honestly that just made me more confused. Think of me as being a monkey, trying to learn rocket science; I'm having a hard time picking up on programming. – JensJakob92 Feb 06 '14 at 12:15
  • You really should not be starting with object oriented programming if you are completely new to programming. – Wajahat Feb 06 '14 at 12:17
  • @Wajahat Try telling my teacher that. If I want to complete my studies, I'll just have to bite the sour apple and hang in there.. – JensJakob92 Feb 06 '14 at 12:18
  • @user3279328 Please read up on this : http://agp.hx0.ru/oop/quarks.pdf I am sure it will help you get started. – Wajahat Feb 06 '14 at 12:20
  • @Wajahat Thank you, I'll try looking at it. – JensJakob92 Feb 06 '14 at 12:23
0

I'd say the best thing you could do is start coding and see what happens. You mentioned that you were worried you would ruin something. You can't break anything by making mistakes, in fact, making mistakes is how all of us learn to do this. Try something and if it doesn't work, the computer will tell you what's wrong with it. Sometimes what the computer tells you doesn't make a lot of sense, but it will at least give you a starting point. The dialog between you and the computer is how you learn what works and what doesn't.

You're also worried about being stuck in something you don't understand. Nobody understands these things at first, and since there's always more to learn than any one person could possibly know, all programmers have to deal with a certain amount of confusion. You have to trust that if you sit down and start working at it that the confusion will clear. The best way to learn a new language is to speak it, and the advantage of programming is computers have infinite patience with your mistakes.

David Stanley
  • 333
  • 3
  • 12
  • Thank you for the wise words. I've tried and tried. And now I've tried to find the solution by just coding something for around 30 minutes, which I thought was correct. Turns out that I still haven't found the solution, and it's driving me crazy. – JensJakob92 Feb 06 '14 at 13:08