Here's the prompt guys:

Assignment #4 will be the construction of 2 new classes and a driver program (the class containing a main method).

Address class

The Address class describes cpu of a bank. It has following attributes:

Attribute name Attribute type Description
city String the city of the bank
state String the state of the bank

The following constructor should be provided to initialize each attribute:
public Address()

This constructor initializes city to "?" and city to "?".

The following accessor methods should be provided to get the attributes:
public String getCity()
public String getState()

The following modifier(mutator) methods should be provided to set the attributes:
public void setCity(String aCity)
public void setState(String aState)

The following method must be defined:

public String toString()

The toString() method constructs a string of the following form:
Tempe,AZ

where Tempe is a city and AZ is a state.

Bank class

The Bank class describes a bank that a customer can create an account. It must have the following attributes:

Attribute name Attribute type Description
bankName String The name of the bank
bankID int The ID of the bank
bankAddress Address The address of the bank
The following constructor should be provided to initialize each attribute:
public Bank()
This constructor initializes all strings to "?", all integers to 0, and instantiates an object of Address (i.e., call the constructor of the Address class to create an object of Address). .

The following accessor methods should be provided to get the attributes:
public String getBankName()
pulbic int getBankID()
public Address getBankAddress()

The following mutator methods should be provided to change the attributes:
public void setBankName(String BankName)
public void setBankID(int anID)
public void setBankAddress(String aCity, String aState)

The following method must be defined:

public String toString()


The toString() method constructs a string of the following format:

\nBank name:\t\tBank of Arizona\n
Bank ID:\t\t1005\n
Bank address:\t\tTempe,AZ\n\n


Upon construction the default values for all String attributes should be set to "?", and all integer attributes to 0


Now I've wrote both classes which I've been getting errors with:

Address Class:

public class Address
{
	private string city;
	private string state;


	public Address()
	{

	 city = "?";
	 state = "?";

	}

	public Address(String aCity, String aState)
	{

	 city = aCity;
	 state = aState;

	}

	public String getCity()
	{

	 return city;

	}

	public void setCity(String aCity)
	{

	 city = aCity;

	{

	public String getState()
	{

	 return state;

	}

	public void setState(String aState)
	{

	 state = aState;

	}

	public String toString()
	{

	 String result;
	 result = + city + "," + state;
	 return result;

	}

}

Here's the Bank class:

public class Bank
{
	private String bankName;
	private int bankID;
	private Address bankAddress;



	public Bank()
	{
	bankName = "?";
	bankID = 0;
	bankAddress = BankAddress(Address);
	}


	public Bank(String BankName, int anID, String BankAddress)
	{
		bankName = BankName;
		bankID = anID;
		bankAddress = BankAddress;
	}

	public String getBankName()
	{
		return bankName;
	}

	public void setBankName(String BankName)
	{
		bankName = BankName;
	}

	public int getBankID()
	{
		return bankID;
	}

	public void setBankID(int anID)
	{
		bankID = anID;
	}

	public Address getBankAddress()
	{
		return bankAddress;
	}

	public void setBankAddress(String aCity, String aState)
	{
		bankAddress = BankAddress;
	}


	public String toString()
	{
		String result;
		result = "Bank name:\t\t" + bankName "\n"
		+ "Bank ID:\t\t" + bankID "\n"
		+ "Bank address:\t\t" + bankAddress "\n\n";

		return result;
	}
}

Am I doing things right? Please help me, your help will be very much appreciated.

Thanks

Recommended Answers

All 9 Replies

You have syntax errors and the error messages should make the problem quite clear.

I get "illegal start expressions" and " ; expected" Could you tell me what I'm doing wrong? It looks right to me...

The line 3 and 4:

private string city;
private string state;

The type of both attributes city and state should be String

It would help us help you if you would post ALL of the error messages you get when you compile the program. The error messages contain lots of useful info about the errors.

I get "illegal start expressions" and " ; expected" Could you tell me what I'm doing wrong? It looks right to me...

I missed that. Well you have more syntax errors than that. So you look at the parse error, find the location, and see that it looks okay. Armed with this knowledge you are, but you must take this knowledge to its logical conclusion. Since it looks okay to you and since the compiler is reporting an error, you know that either the compiler is wrong, or your knowledge of what looks okay is somehow corrupted. The compiler is going to be right, especially for simple languages like Java. That means your assessment of the code is wrong. No big deal, happens all the time. But why is it wrong? It looks so right. We see private string city; . What could be wrong with that? We know that you can declare variables at the beginning of the class. And we know that the syntax declaration for a variable can be of the form private <type name> <variable name>; . And we see that you spelled private correctly. And city is a valid variable name, since it's just a lowercase word, and certainly not some kind of keyword (but if you weren't sure, you could check). So could string be the culprit? Maybe. How could that be wrong, "string" seems perfectly okay. So now you go and investigate. Is "string" really okay? What if you made a smaller class with only the private variables. Is there still a problem with the declaration in that case? Okay, there is. So it's not some weird confounding error, probably. Let's look at other code examples. Do other classes, that do compile, declare string variables they way you do? No, they don't. Ah, silly you, misspelling String.

Why didn't you do this? Why is "It looks right to me" all the investigation you could make into the problem? Do you really take problems requiring exact precise logical thought, say "it looks right to me," and call it a day? You don't go back to the basic assumptions upon which your knowledge of the subject matter is based and work your way back up, to double-check? Do you even keep track of what your assumptions are?

Recently I have made a simple chatting application(in Java).
It has two classes one for client and one for server.On compiling it is not giving any error but on running, it is not giving desired result.

So, It will be grateful if you come ahead and try out helping me in any way or solving/correcting my program


Client class is:

package com;

import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ChatClient {
    JTextArea in;
    JTextField out;
    JButton button;
    BufferedReader reader;
    PrintWriter writer;
    Socket sock;

    public static void main(String ar[]) {
        ChatClient client = new ChatClient();
        client.go();
    }

    public void go() {
        JFrame frame = new JFrame("Chat Application");
        JPanel panel = new JPanel();
        in=new JTextArea(15,50);
        in.setLineWrap(true);
        in.setWrapStyleWord(true);
        in.setEditable(false);
        JScrollPane scroller = new JScrollPane(in);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        out=new JTextField(20);
        button=new JButton("SEND");
        button.addActionListener(new ButtonListener());
        panel.add(scroller);
        panel.add(out);
        panel.add(button);
        setUpNetworking();

        Thread readerThread = new Thread(new IncomingReader());
        readerThread.start();

        frame.getContentPane().add(BorderLayout.CENTER,panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,500);
        frame.setVisible(true);
    }

    public void setUpNetworking() {
        try {
            sock = new Socket("127.0.0.1",5000);
            InputStreamReader stream = new InputStreamReader(sock.getInputStream());
            reader = new BufferedReader(stream);
            writer = new PrintWriter(sock.getOutputStream());
            System.out.println("Network Established");
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }

    public class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            try {
                writer.println(out.getText());
                writer.flush();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            out.setText(" ");
            out.requestFocus();
        }
    }

    public class IncomingReader implements Runnable {
        public void run() {
            String message;
            try {
                while((message=reader.readLine())!=null) {
                    System.out.println("raed "+message);
                    in.append(message+ "\n");
                }
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    }

}

Server class is:

package com;

import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer {
    ArrayList clientOutputStream;

    public static void main(String ar[]) {
        new ChatServer().go();
    }

    public void go() {
        clientOutputStream = new ArrayList();
        try {
            ServerSocket serverSock = new ServerSocket(5000);
            while(true) {
                Socket clientSocket = serverSock.accept();
                PrintWriter writer = new PrintWriter (clientSocket.getOutputStream());
                clientOutputStream.add(writer);

                Thread t = new Thread (new ClientHandler(clientSocket));
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public class ClientHandler implements Runnable {
        BufferedReader reader;
        Socket sock;

         public ClientHandler(Socket clientSocket) {
             try {
             sock=clientSocket;
             InputStreamReader stream = new InputStreamReader(sock.getInputStream());
             reader = new BufferedReader(stream);
             }
             catch(Exception e) {
                 e.printStackTrace();
             }
         }

         public void run() {
             String message;
             try {
                 while((message=reader.readLine())!=null) {
                     System.out.println("read "+message);
                     tellEveryOne(message);
                 }
             }
             catch(Exception e) {
                 e.printStackTrace();
             }
         }

         public void tellEveryOne(String message) {
             Iterator it = clientOutputStream.iterator();
             while(it.hasNext()) {
                 try {
                     PrintWriter writer = (PrintWriter)it.next();
                     writer.println(message);
                     writer.flush();
                 }
                 catch(Exception e) {
                     e.printStackTrace();
                 }
             }
         }
    }
}
commented: Start new thread for your new topic +0

Dupron
Please start your own thread instead of hijacking someone else's.

I've compiled the Address code and had it successful. I didn't notice it before so that's why it threw me off. I'm having trouble "instantiate an object of Address" as stated below. I've initialized all string and int accordingly, except for the "bankAddress" because i need to instantiate it as an object of Address. I'm not sure how.


This constructor initializes all strings to "?", all integers to 0, and instantiates an object of Address (i.e., call the constructor of the Address class to create an object of Address).

public Bank()
	{
	bankName = "?";
	bankID = 0; 
	bankAddress = BankAddress(Address); // <------ Is this right? Or am I instantiating it wrong?

}

Too little code to know what the variables and methods you show do.
For example:
What data type is bankAddress?
What data type is Address?
What does the method: BankAddress() take for an argument?
What does the method: BankAddress() return?

To create an instance of a class use new CLASSNAME...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.