Feel free to post questions and comments regarding the Celestial Bodies lab assignment here.
Common Problem from Pre-lab
After grading the pre-lab assignments, I think a little clarification is needed.
1. static variables belong to the Class, you do not need an instance of that class to access it.
2. private access only allows visibility of a method or member variable within the declared class. All nested classes have visibility because they are inside the class. However, you do not have access within the entire file.
3. private member variables have no visibility outside the class. To access it you must write a method that is non-private that accesses it. This provides an interface to allow someone to use and manipulate what is inside your class.
4. a static method is accessed through the class (i.e Math.pow()). Again, no instance is required. A non-static method is accessed through an instance of the class. (i.e. Object j = new Object; System.out.println(j.toString()).
Best of luck!
Joe
Where exactly do we access the Car.java and CarDemo.java? I'm having trouble remembering how to get there.
ReplyDeleteOne way to access these files is to use SVN to checkout the current revision. In this case, just type "svn checkout file:///nfs/faculty/hayes/pub-svn" from the terminal when logged into your CS account (locally in the lab or remotely via ssh). You will now have a copy of everything Hayes' has made available. If you know the specific directory you want to checkout, use its path in the svn checkout command.
DeleteThe files will show up in your current working directory, inside a new directory titled the same as the bottom-level directory specified in the svn checkout command.
Rob is correct. As stated in the write up, "both are in the package edu.unm.cs251.s2012.lab.feb1 within the repository".
DeleteOnce you have it checked out, you can navigate to that package from the terminal by typeing "cd edu/unm/cs251/s2012/lab/feb1"
Joe
Do you happen to use Windows? You can add the path of your JDK to the path environment variable which allows you/programs to execute java/javac/javadoc commands from the Windows command prompt.
ReplyDeleteGo to Advanced System Settings (if you're on Vista or newer, just start typing the first few letters of "advanced" from the start menu and you'll see a link popup).
Click the link. A window will popup. Click "Environment Variables".
Under "System Variables", double-click on "Path" and prepend the full path of where the java and javac executables are located on your system, terminated by a ";". You need not include a trailing \ character in the path.
Now you can use programmer-oriented text editors (with support for executing shell/console commands) to create macros to compile/run/javadoc your code. I personally use Notepad++ with the NppExec plugin, since remote SSH+X11+EMACS is unacceptably slow for me.
Studs Only:
Run a svn server instance on your dedicated home server, buy a domain (.info is cheap), and leave your flash drive at home!
I'm having trouble understanding how packages work? When Tom was explaining it, it went completely over my head. Is there a website explaining it in more detail?
ReplyDelete-Alexander Moon
In short, packages are a way to organize Class files. For example, the java.util package contains various "utility" classes.
DeleteOne reason there is this strange "edu.unm" package structure is so that it is easy to tell who wrote a class you are using. Imagine there are two people writing a class named "Body". One person is using this to write a class used for a medical program. The other uses the class to represent a land mass in the ocean. If there were no package structure, you would be unable to write a program using both of these classes
Also, if you google "Java Packages" you will probably find additional information.
Thanks.
DeleteJust a quickie: are we measuring the mass of the objects in Earths, or are we using kg? The reason I'm asking is because our objects have to have a mass, yet the getMass() method is supposed to return mass in Earth masses.
ReplyDeleteI believe the masses are in earths.
DeleteThat is correct. According to the resources I've looked at, the correct way to measure mass of celestial bodies is in Earths.
DeleteI have been having trouble getting used to the text editor. I have been doing fine but it takes so long and when it came to turning in the IntCalc.java I just could not figure it out. In the end I just copied it to eclipse and used that .java file. I have been reading up and hopefully I wont feel like as big a tool.
ReplyDeleteIt can be very tough when you start using a tool such as emacs or vi. I recommend googling a tutorial to teach you how to more effectively use one. However, to get the file off of the CS machines, you actually need to use another program. If you are on a mac or linux device you can use the scp command from the terminal on your machine (not over the network). If you are on Windows, you will need to get a 3rd party application that does ftp or sftp. Try googling for "free ftp client".
DeleteHow can we test our celestial body in the main method?
ReplyDeleteI believe we will be accessing our CelestialBody.java file via another file with a main method. From there you can try to instantiate some bodies and use their methods to find out if the info is being stored properly by printing to the terminal.
DeleteI'm sure this means we will need a proper package set up, too.
Zach is correct. I want you to write a main method (in another class) to test the functionality of your CelestialBody class. This is 100% for your benefit and should not be turned in.
DeleteTwo questions that I have on my pre-lab, but I will reiterate here:
ReplyDelete1) What is meant by the getOrbiting() method being untestable? In our discrete math class we recently talked about sets of recursive functions; I think this has something to do with it.
2) If I am creating static objects of the CelestialBody type to keep track of the smallest/largest bodies, if I set one of these objects to equal another body that already exists, am I creating a duplicate of the data, or am I creating a reference to that body? As someone who is used to using malloc(), this kind of stuff bothers me.
1) It is not testable because the functionality is undefined. The write up states, " If the isOrbiting() method returns true this method returns the CelestialBody that it is orbiting, otherwise the functionality is undefined (not testable)." This means that if is Orbiting() returns true then I can test to see if getOrbiting() returns correctly. if isOrbiting() does not return true, the method may do anything. It is not defined what it does and therefore it is not a testable requirement.
Delete2) You're just changing where your smallest/largest reference points to so that it references the body you set it to.
DeleteI am also having issues with the getOrbiting() method. I don't really understand what is meant by not testable. At first I just thought that I would return null if isOrbiting() returned false but that does not seem to make it "not testable" it just makes it return useless. Any elaborations would be helpful.
DeleteNot Testable simply means that us (the TAs) will not be able to test what it does. It can do anything. Generally, an exception would be thrown but we have not covered them in class yet. Returning null is fine.
DeleteJoe thanks for clarifying the issue above. One more question. Is it possible to just create an array of type CelestialBody or does this require a much more intensive constructor?
DeleteYou can make an array of type CelestialBody by doing the following:
DeleteCelestialBody[] bodies = new CelestialBody[n];
where n is the size of the array you would like to create.
This will work for any object. Initially, all indices will be "null".
hello,
ReplyDeleteok i think there is an issue with my PATH..though im not sure the error i get when trying compile in emacs is "/bin/bash: test.java: command not found" i know the file working because it was tested on a CS lab computer. I dont know if its a path error or something that im not thinking of.
thanks
Derek
Hmmm... it looks like you are trying to run a terminal in emacs. To run a shell command via emacs press Meta - !. Meta is usually the Alt key by default. This should bring up a little prompt that says "shell". If you type Meta - x and then type terminal it will ask which terminal you want to run. The default on the cs machines is "/bin/bash" you can simply hit enter. Another option is to use Meta - x and type shell. This creates an emulated shell that allows you to still send keyboard commands to emacs.
DeleteHope this helps.
I'm having trouble with my static methods.
ReplyDeleteFor getLargestBody(), should I call a method from Java APIs that can find the largest object field out of the whole class? or somehow build an array of all the objects' mass and sort it?
For getTotalBodies(), I initialized ID and numberBodies fields like so:
private int id;
private static int numberBodies;
and then I assign an ID to each object with:
this.id = numberBodies++;
So far this works to assign unique IDs to my objects, but when I run the static method of getTotalBodies() I get a + 1 to the actual total. (I wrote a separate main method that only contains 2 objects, but gives me 3 objects when I call getTotalBodies()).
any hints would help. Thanks
I'm having trouble with the getLargestBody and getSmallestBody as well.
DeleteRoger, you shouldn't need to use any of the classes in Java's main API to accomplish this (if you do it correctly). Let's think about which CelestialBody is the largest / smallest.
DeleteInitially, you have no largest and smallest. Perhaps the methods return null OR some special CelestialBody that represents the lack of a CelestialBody.
The first time you instantiate a CelestialBody, that body *must* be both the largest and the smallest.
The nth time you instantiate a CelestialBody, you must check to see if it is larger than the largest or smaller than the smallest. If it is, you must replace it.
One down side to this it you can only have one CelestialBody that is the largest or smallest. You can't have two that are equal. However, the spec requires that you return just one.
As for your getTotalBodies() method, if it always returns 1 greater than the amount that exist, couldn't you simply subtract 1?
Joe, I see what your saying about getLargestBody() method. For example, initialize a static largestBody field, and then compare it with each new object's mass to see if it "takes the heavyweight title" away.
DeleteAs for getTotalBodies() method, I could subtract 1 and that would work. But I still want to know what the computer is doing, because I don't think it should be doing that. The numberBodies initializes at 0 (I also assigned 0 to check).
Thanks
Roger
Roger, if you could email me directly (jcollard [at] unm [dot] edu) with your source code I might have a better answer for you. The only thing I can think from the data provided is that you have added an extra 1 to your total bodies value somewhere along the way.
DeleteJust wanted to clarify a few things.
ReplyDelete-Are we supposed to prompt the user to input a name and mass for the celestial body, or is this something that will be random?
-Are we even supposed to have an output for this program? We have a lot of 'get' methods to gather information on the celestial bodies, but I'm confused as to what to do with that data.
You are simply writing a stand alone class. This class is not executable in anyway. If you type "java CelestialBody" you should get an error saying no main method was found. However, it is highly recommended you write a "Driver" class that has a main method and uses the CelestialBody. Think back to the CelestialBody and GalacticDriver classes we wrote in class. I believe these are both in the repository.
DeleteDoes this help?
Yeah, this clears some things up. Thanks!
DeleteHaving one more issue...
DeleteWhen I use any of the methods that return a Celestial Body object, how can I print the name of the object instead of it's memory address. Could I use a toString method, or can we permit the getLargestBody/getSmallestBody methods to return a string?
Well, by default the toString() method prints some "garbage". But, you can always access the methods. For example you could print body.getName().
DeleteDoes that help?
I didn't occur to me to try that. I'll give it a shot.
DeleteI was actually wondering the same thing.. The assignment says: "getLargestBody() returns the CelestialBody that has the largest mass out of all CelestialBody instances that have been created." So is it okay if we're returning just the name (string) of the largest/smallest celestial bodies despite it asking us to return a CelestialBody?
DeleteNick, that is not sufficient. It specifically states it must return a CelestialBody. A String lacks all of the information about a CelestialBody. For example, it would be nice to use getLargestBody() then check each of its getters to know more about it.
DeleteAlright, thanks.
DeleteWoops, just realized it was a problem with my driver that was confusing me.
DeleteI'm having trouble with the mass part. It says in the lab that we need to return the mass in Earths. When we create the CelestialBodyDemo to test, is the mass in kg and we are converting? I have a feeling this probably really obvious, but I'm just not getting it.
ReplyDeleteJanetta, the "Earths" part is really arbitrary. As long as mass is a double you should be fine.
DeleteAlso, regarding the getID, are we supposed to randomly generate a number and assign it to the celestial body?
ReplyDeleteIf you assign a random ID there is a chance they will not be unique. Each body must have a unique ID. If you assign them in order of creation this will be true. For example, the first CelestialBody has an id of 1, the second an id of 2, the third and id of 3, etc.
DeleteAh ok, thanks
DeleteOne more thing...
ReplyDeleteWhen it comes to packages, do we need to include the following line in the code of the CelestialBody class?
" edu.unm.cs.cs251spr12.batch.lab.feb1 "
No, not for this lab.
DeleteI can't reach moons.cs.unm.edu through putty or through ssh via linux.unm.edu, I tried yesterday, this morning and today. Although I can reach it through an ftp client, so I was able to get my work off the server.
ReplyDeleteAny idea why this is happening?
Try accessing "trucks.moons.cs.unm.edu".
Delete