aspose file tools*
The moose likes Beginning Java and the fly likes calling a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling a class" Watch "calling a class" New topic
Author

calling a class

Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Hi
Can someone tell me how to run a class from inside another program? I have a program that works and have to add a new class, but I dont know how to get the original program to run the new class.

Can you help please!!!
PS I am using JBuilder (been told to)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not sure what you mean by "run a class." You create instances of classes, and call methods.

Basically, in your new class, just create an instance of the original class and then call whatever method(s) you need. Note that you can call main just like any other method, as long as you provide a String array.

If your original class is in another package, then you will need the proper import statement in your new file.

Give it a try.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
If you are trying to create an instance of a class from another package, make sure your class has public access. Default access doesn't allow that.

You can read more about access modifiers at:

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Can you post some of your code here? It would be easier to help.
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Thanks Marc
I dont think I explained it very well, I am VERY raw at Java and been at this for nearly 11 hours

I need to create a class (within JBuilder) and then add this into another program.
the task says
add a nurse class to the project,
define 2 attributes for nurse class,
define one piece of behaviour for nurse class,
write a nurse constructor,
write code to create a nurse object in the main method and store a referene to that nurse object,
write code to send a messaghe to the nurse object.

Here is what I have so far
package Admin;

import javax.swing.*;

public class Admin{

static int numberOfStaff;
static String[]staffNames;
public Admin(){
}
public static void main(String[]args){
Admin.setThingsUp();
System.out.println("All Data Entered Goodbye");
System.exit(0);
}
private static void setThingsUp(){
while(numberOfStaff < 1 || numberOfStaff > 3)//loop to ensure only 1-3 staff can be entered
{
String userln=JOptionPane.showInputDialog("Enter the number of Nurses presently employed. 3 Maximum");
numberOfStaff=Integer.parseInt(userln);
}
{
staffNames = new String[numberOfStaff];//this added to initialize the array

for (int count = 0 ; count < numberOfStaff; count=count + 1)
{
staffNames[count]=JOptionPane.showInputDialog("Enter the name of the next Nurse.");
continue;
}
}
numberOfStaff = integer.parseInt(userIn);
staffNames=new String[numberOfStaff];
}
}

Any ideas please?
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Sorry Lajos
I missed your reply
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
OK thanks
Can anyone help with this please.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Let's start with step one of your assignment.


add a nurse class to the project,


Can you show us what you did so far in regards to creating this new class?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Hi Henry
I was provided with the code.
I have done simple classes, but I dont know how to call one from inside the code. I wanted to add a class that allows the input of natInsNo and staffID along with the number of staff and staff names.
Dont know if this helps?
thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Wayne,

Take one step at a time -- for now, just do the first step of the assignment.

Create a class called "nurse" -- and show us the code please.

Henry
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Hi Henry
this is as far as I can go, I get stuck then
public class Nurse{
String NurseID; //employee id
String NatIns; // National Insurance Number

public Nurse (String NurseID[], String NatIns[]){

NurseID= new NurseID[];
NatIns= new NatIns;[];

NurseID[count]=JOptionPane.showInputDialog("Enter the Nurse ID Number.");
NatIns[count]=JOptionPane.showInputDialog("Enter the National Insurance Number.");}
{
{
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
You are missing a couple things here ands there.. It looks like you need an array of String, so I would start by correcting the variable declaration and assignment.

I assume you are initializing this object with a nurse and insurance array you already have somewhere else...



public class Nurse {

String[] NurseID;
String[] NatIns;

public Nurse(String[] NurseID, String[] NatIns ) {
this.NurseID = NurseID;
this.NatIns = NatIns;
}
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
Hi thanks for getting back
I am really raw to this and its driving me mad. I dont know what on earth you are talking about really.
I have had about 2 hours in tutition on this and now given this task. I have been at it all day searching etc and cant do it.

If you look at the top of this thread you can see the problems I need to do. I dont want people to do it for me, but an example etc would be great.
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
Make sure that your public Nurse class is in a file called Nurse.java.

You might need a package statement at the top of the Nurse.java file, but in
order to help with that you need to tell us 1. what directory Nurse.java is in 2. the CLASSPATH settings for your project.

Once you have this file saved you can make instances of it from another class, e.g.:


...
String[] NurseID ={"mary","jane"};
String[] NatIns = {"ma54","ja67"};
Nurse nurse = new Nurse(NurseID,NatIns);
...

You might need an import statement in the file you are instancing Nurse from, but for that we need to know your project setup (see questions 1 and 2 above.)
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
OK
ver java 1.3.0_02
C:/Documents and Settings/Wayne/jbproject/Admin/classes

I am using the defaults
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Originally posted by Wayne Styles:
Hi thanks for getting back
I am really raw to this and its driving me mad. I dont know what on earth you are talking about really.
I have had about 2 hours in tutition on this and now given this task. I have been at it all day searching etc and cant do it.

If you look at the top of this thread you can see the problems I need to do. I dont want people to do it for me, but an example etc would be great.


Go back and reread everything you have up to this point. Don't search for a solution, search for the concepts and understand it. Then writing code will come a bit easier.

Yeah, reading is a bit dry but is infinitely better then getting key prints on your forehead.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
Can you give the full path to both Nurse.java and the .java file you are instancing Nurse from?
Wayne Styles
Ranch Hand

Joined: Aug 18, 2006
Posts: 45
C:\jbuilder5\jdk1.3\bin\javaw -classic -classpath "C:\Documents and Settings\Wayne\jbproject\Admin\classes;C:\jbuilder5\jdk1.3\demo\jfc\Java2D\Java2Demo.jar;C:\jbuilder5\jdk1.3\jre\lib\i18n.jar;C:\jbuilder5\jdk1.3\jre\lib\jaws.jar;C:\jbuilder5\jdk1.3\jre\lib\rt.jar;C:\jbuilder5\jdk1.3\jre\lib\sunrsasign.jar;C:\jbuilder5\jdk1.3\lib\dt.jar;C:\jbuilder5\jdk1.3\lib\tools.jar"

Is this what you want.

As for the reading bit....I only have online resources to chase...
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
Also need the path where you store Nurses.java and the .java file instancing Nurses.
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
That is where you are going wrong. Chasing down bits and pieces to learn a language is inefficient and frusterating.

Here are two good resources:

Lots of tutorials starting from scratch:

http://java.sun.com/javase/reference/tutorials.jsp

If that link doesn't work, go to java.sun.com.

Here is a free online book and is generally considered to be one of the better books for beginners:

http://www.mindview.net/Books/TIJ/

The 4th ed, doesn't seem to be free in its entirety, but the first 7 chapters will get you pretty far, and most, if not all, of the 3rd edition will still be valid.

Sorry for not giving hints and/or solution to your problem, I just think that this will be far better for you in the long run.
[ August 20, 2006: Message edited by: Rusty Shackleford ]
 
 
subject: calling a class