Skip to main content

Beginner java questions

3 replies [Last post]
Alysia
Offline
Joined: 2006-02-17

A friend and I are learning java on our own using BlueJ. We are going over the exercises in the book and are having a couple of problems with the exercise below.

Here are our problems:
1. Modify your printDetails method to include printing the reference number. However, the method should print the reference number only if it has been set -- that is, the refNumber string has a non-zero length. If it has not been set, then print the string "ZZZ" instead. hint: use a conditional statement whose test calls the length method on the refNumber string.

ok so I did it one way (see below) and it works but I don't think I did it the way they wanted me to.

2. Modify your setRefNumber mutator so that it sets the refNumber field only if the parameter is a string of at least three characters. If it is less than three, then print an error message and leave the field unchanged.

this question we toatlly do not understand. can someone explain how we do this?

/**
* A class that maintains information on a book.
* This might form part of a larger application such
* as a library system, for instance.
*
*/
class Book
{
// The fields.
private String author;
private String title;
private int pages;
private String refNumber;

/**
* Set the author and title fields when this
object
* is constructed.
*/
public Book(String bookAuthor, String bookTitle,
int new_pages)
{
author = bookAuthor;
title = bookTitle;
pages = new_pages;
refNumber="";
}

// Add the methods here ...

public void printAuthor()
{
System.out.println("**********");
System.out.println(author);
System.out.println("**********");
}

public void printTitle()
{
System.out.println("**********");
System.out.println(title);
System.out.println("**********");
}

public int getPages()
{
return pages;
}

public void setPages(int new_pages)
{
pages = new_pages;
}

public void setRefNumber(String ref)
{

refNumber = ref;
}

public String getRefNumber()
{

return refNumber;
}

public void printRefNumber()
{
if (refNumber !="") {
System.out.println("**********");
System.out.println(refNumber);
System.out.println("**********");
}
else {
System.out.println("**********");
System.out.println("ZZZ");
System.out.println("**********");
}
}
}

Message was edited by: Alysia

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vdkuil
Offline
Joined: 2006-02-17

This is a default problem.... Objects (and string is an object) can never be compared using ==... Try using equals("") instead..

or try myString.length() == 0

Adidas03
Offline
Joined: 2006-02-17

Please someone, could you help another beginner, I have the same question on Alysia's question 2, how do I do that?

jordihs
Offline
Joined: 2004-05-20

You are supposed to modify the method setRefNumber(String ref) in such way that if the parameter (ref) is less than three chars long you write a System.out statement with some made-up error message and you don't set the value of refNumber (that is, refNumber = ref; is not executed).