Jump to content

Importing my own class file?

Tohrchur

Hey guys I'm having a bit of trouble. I made a whole giant class file called "lab2" that has a bunch of methods in it. And part of the assignment is to also create a driver program to test all the methods. I can't figure out how to get my lab2 class file imported into my lab2Test main class to test it. 

If it matter, both of the .java files are in the same folder.

Any help is appreciated!

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

They should both be in the same package too.

Gaming - Ryzen 5800X3D | 64GB 3200mhz  MSI 6900 XT Mini-ITX SFF Build

Home Server (Unraid OS) - Ryzen 2700x | 48GB 3200mhz |  EVGA 1060 6GB | 6TB SSD Cache [3x2TB] 66TB HDD [11x6TB]

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, suchamoneypit said:

They should both be in the same package too.

I've never messed with packages before. Do I just have to put at the top like "package myPackage;"?

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Tohrchur said:

I've never messed with packages before. Do I just have to put at the top like "package myPackage;"?

yup

 

I wrote several programs for my java programming class where we made a class with the methods, and used a separate class to test them all.

Gaming - Ryzen 5800X3D | 64GB 3200mhz  MSI 6900 XT Mini-ITX SFF Build

Home Server (Unraid OS) - Ryzen 2700x | 48GB 3200mhz |  EVGA 1060 6GB | 6TB SSD Cache [3x2TB] 66TB HDD [11x6TB]

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, suchamoneypit said:

yup

Tried that. I get this error.

mV3GF.png

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, suchamoneypit said:

yup

I put them both in package "myPackage" and then on the test file i put "import lab2;" and tried "import myPackage.lab2;" both with the same error. :(

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Tohrchur said:

Tried that. I get this error.

mV3GF.png

you should just have to type this:

 

package matthewA;

 

in your case

 

package lab2;

 

 

 

and put that in the top of both class files. You don't import anything. Your just telling it what package its part of. Now keep in mind that lab2 is a class file. in whatever thing your using (eclipse?) you need to create a new package, name its "Labs" for example, then you can drag an drop lab1 and lab2 .java files in there, open them, and add that code in the top. If you right click the package and create a new class file it will automatically put this code in for you.

Gaming - Ryzen 5800X3D | 64GB 3200mhz  MSI 6900 XT Mini-ITX SFF Build

Home Server (Unraid OS) - Ryzen 2700x | 48GB 3200mhz |  EVGA 1060 6GB | 6TB SSD Cache [3x2TB] 66TB HDD [11x6TB]

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, suchamoneypit said:

you should just have to type this:

 

package matthewA;

 

 

 

and put that in the top of both class files.

This is whats on top of my test file.

package myPackage;
import java.util.*;
import java.lang.*;
import lab2;

 

and this above my class file:

package myPackage;
import java.util.*;
import java.util.Arrays;
import java.lang.Math.*;
public class lab2{

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Tohrchur said:

This is whats on top of my test file.

package myPackage;
import java.util.*;
import java.lang.*;
import lab2;

 

and this above my class file:

package myPackage;
import java.util.*;
import java.util.Arrays;
import java.lang.Math.*;
public class lab2{

is there any way you could download a program like puush and provide screenshots to better show me the issue?

Gaming - Ryzen 5800X3D | 64GB 3200mhz  MSI 6900 XT Mini-ITX SFF Build

Home Server (Unraid OS) - Ryzen 2700x | 48GB 3200mhz |  EVGA 1060 6GB | 6TB SSD Cache [3x2TB] 66TB HDD [11x6TB]

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, suchamoneypit said:

is there any way you could download a program like puush and provide screenshots to better show me the issue?

this is the class file

mV4en.png

 

 

this is the test file

mV4gi.png

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Also that is not all of the class file, its like 200 lines long, i just screenshotted the first part.

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

hmm, Im unfamiliar with whatever program your using to code, but it should be working if the package is set up properly because the code seems correct, although im still not sure why your are importing lab2.

 

There could be something happening here I do not understand as I am by no means an advanced coder.

 

@PlutoNZL

@Jedi

@elpiop

@Nuluvius

 

(hopefully that properly notifies it, these people have helped me before)

Gaming - Ryzen 5800X3D | 64GB 3200mhz  MSI 6900 XT Mini-ITX SFF Build

Home Server (Unraid OS) - Ryzen 2700x | 48GB 3200mhz |  EVGA 1060 6GB | 6TB SSD Cache [3x2TB] 66TB HDD [11x6TB]

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, suchamoneypit said:

hmm, Im unfamiliar with whatever program your using to code, but it should be working if the package is set up properly because the code seems correct, although im still not sure why your are importing lab2.

 

There could be something happening here I do not understand as I am by no means an advanced coder.

I had the import lab2 to try it because nothing was working. I am using jGrasp but i will try others. Thank you!

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

Public classes which are in the exact same package do not need to be imported into each other. Remove the import statements and try calling the lab2 class within the lab2Test class again and tell us what happens. 

 

Furthermore, right now it seems like none of your classes are physically inside packages (folders).

When you put a class in a package, that package structure is represented by folders, or directories.

That means if you put a class in a myPackage package, then you must also put the physical .java file in a folder titled "myPackage" (without quotes or punctuation). And if you have a class in, say, the myPackage.test.main package, then you'll need to put the class inside a folder titled "main," which is in turn inside a folder titled "test," which is in turn inside a folder titled "myPackage."

Link to comment
Share on other sites

Link to post
Share on other sites

Figured it out last week, thought I'd post incase someone else comes across that. 

I had to create the function.  I had to do:

lab2 function=new lab2();

then all of my methods were function.MethodHere;

Tohrchur

Link to comment
Share on other sites

Link to post
Share on other sites

19 hours ago, Tohrchur said:

Figured it out last week, thought I'd post incase someone else comes across that. 

I had to create the function.  I had to do:

lab2 function=new lab2();

then all of my methods were function.MethodHere;

I think that in order to progress further, you should get all the terms straightened out. What you have labeled as "function" is actually not a function, but an object. They're two very different things. 

 

Here is a syntax representation of exactly what is what:

Class object = new Class();

object.function();
object.method();

A class is a type of object, and an object is a single instance of that class. For example, Human is the class and a person is the object:

Human michael = new Human();
Human joe = new Human();
Human melissa = new Human();

michael.run();
joe.walk();
melissa.swim();

In this case, we usually don't think of Michael or Joe or Melissa as a "function" or an "action" - each of those people is a physical thing, an object. 

 

On the other hand, running, walking, and swimming are actions, things you can do. That's why they're called functions. Also keep in mind that method = function. They're two different names for the same thing. It's just that some languages such as Java call it method and other languages such as C++ call it function.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×