Results 1 to 2 of 2
  1. #1
    vcardell is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Problem threading?

    This is what I'm trying to do "You will develop a multithreaded client/server application. The client will send an Account object to the server. The server will calculate the interest for the object (5%), set the balance to the new amount, and return the object to the client. The client will then print out the account number and balance. The account class has attributes name, account number, and balance and must adhere to our coding standards.

    Use ExecutorService and provide output statements showing the progress of the transaction on both the client and server sides."

    I keep getting an error when I try and compile my ThreadServer about the Account class - about an invalid method declaration; return method type required..

    I posted my Account class and client and server codes below..

    Any help would be appreciated...


    Thanks,





    Java Code:
    import java.io.*;
    public class Account implements Serializable
    {
    
    	private String name;
    	private int accountNumber;
    	private double balance;
    
    	public class Account()
    	{
    		setName("");
    		setAccountNumber(0);
    		setBalance(0);
    	}
    	public class Account(String n, int a, double b)
    	{
    		setName(n);
    		setAccountNumber(a);
    		setBalance(b);
    	}
    	public void setName(String n)
    	{
    		name = n;
    	}
    	public void setAccountNumber(int a)
    	{
    		accountNumber = a;
    	}
    	public void setBalance(double b)
    	{
    		balance = b;
    	}
    	public String getName()
    	{
    		return name;
    	}
    	public int getAccountNumber()
    	{
    		return accountNumber;
    	}
    	public double getBalance()
    	{
    		return balance;
    	}
    	public String toString()
    	{
    		return( " Name " + name + " Account Number " + accountNumber + " Balance " + balance);
    	}
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.Scanner;
    import java.util.Formatter;
    import java.util.concurrent.*;
    
    public class Client2
    {
    	public static void main(String args[])
    	{
    		
    		Scanner myInput = new Scanner(System.in);
    		
    		Socket connection;
    		ObjectInputStream input;
    		ObjectOutputStream output;	
    		String name;
    		int accountNumber;
    		double balance;
    
    
    		Object obj;
    
    		
    	
    		try 
    		{
    			connection = new Socket("localhost", 8000);
    			
    			input = new ObjectInputStream(connection.getInputStream());
    			output = new ObjectOutputStream(connection.getOutputStream());
    			
    			
    			System.out.println("Enter Account Holder's Name");
    			name = myInput.nextLine();
    			
    			System.out.println("Enter Account Number");
    			accountNumber = myInput.nextInt();
    			
    			System.out.println("Enter Account Holder's Name");
    			balance = myInput.nextDouble();
    			
    			obj = (Object) input.readObject();
    			output.writeObject(obj);
    			output.flush();
    		
    			//obj = (Object) input.readObject();
    			System.out.print(obj.toString());
    		}
    		catch(ClassNotFoundException cnfe)
    		{
    			cnfe.printStackTrace();
    		}
    		catch(IOException ioe)
    		{
    			ioe.printStackTrace();
    		}
    	}
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    import java.util.Scanner;
    import java.util.Formatter;
    import java.util.concurrent.*;
    
    //.The server will calculate the interest for the object (5%), 
    //set the balance to the new amount, and return the object to the client.  
    
    public class ThreadServer
    {
    	public static void main(String args[])
    	{
    		
    		
    		ServerSocket serverSocket;
    		Socket connection;
    		
    		
    	
    		try
    		{
    
    			serverSocket = new ServerSocket(8000);
    		
    			ExecutorService threadExecutor = Executors.newCachedThreadPool();
    		
    			int clientNo = 1;
    		
    			while (true)
    			{
    		
    				connection = serverSocket.accept();
    			
    				System.out.println("Start thread for client" + clientNo);	
    		
    				HandleAClient thread = new HandleAClient(connection, clientNo);
    
    				threadExecutor.execute(thread);
    
    				clientNo++;
    			}
    		}
    		catch(IOException ioe)
    		{
    			ioe.printStackTrace();
    		}
    	}
    }
    
    class HandleAClient implements Runnable
    {
    
    			
    		ServerSocket serverSocket;
    		ObjectInputStream input;
    		ObjectOutputStream output;
    		Socket connection;
    		//Formatter output;
    		int clientNo;
    		double balance;
    		
    		Object obj;
    		
    		public HandleAClient(Socket connection, int clientNo)
    		{
    			this.connection = connection;
    			this.clientNo = clientNo;
    		}
    		
    		public void run()
    		{
    			try 
    			{
    				serverSocket = new ServerSocket(8000);
    			
    				connection = serverSocket.accept();
    			
    				output = new ObjectOutputStream(connection.getOutputStream());
    				input = new ObjectInputStream(connection.getInputStream());
    		
    				obj = (Object)input.readObject();
    
    				balance = ((Account)obj).getBalance()*1.05;
    				
    			
    				System.out.println(obj.toString());
    			
    				output.writeObject(obj);
    				output.flush();
    			}
    			
    			
    			catch(ClassNotFoundException cnfe)
    			{
    				cnfe.printStackTrace();
    			}	
    			catch(IOException ioe)
    			{
    				ioe.printStackTrace();
    			}
    		}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default Re: Problem threading?

    Hint: a constructor doesn't have a return type.

    A method does, but it must be a recognizable return type. The keyword void is allowed for methods that don't return anything. The keyword class isn't.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Metronom app - problem with threading
    By dejmien666 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-27-2011, 08:25 AM
  2. Threading problem
    By Andyj in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-05-2010, 09:27 PM
  3. Problem in Multi threading.
    By Chetans in forum Advanced Java
    Replies: 3
    Last Post: 03-23-2010, 04:42 PM
  4. Getting problem in threading in JAVA
    By Chetans in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-19-2010, 07:49 AM
  5. Problem in threading
    By saurabh in forum Threads and Synchronization
    Replies: 6
    Last Post: 12-01-2008, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •