Question about Compilers & Programming Tools

1 Answer

Java gui code

Ok i am exhausted attempting to puch usless info into my brain! My LAST CLASS before graduation is basic java but it is anything but basic - I am in need to understand and find code to list products in an generated inventory that is input by the user and given a cost and quantity - the teach just keep throwing code at us and not explaining what they are for!! im am totally lost and frustrated... can ANYONe point me to a site that has classes in a library that we can pull and manipulate to work (i can DO that ) as the real programmers do?

Posted by on

1 Answer

  • Level 2:

    An expert who has achieved level 2 by getting 100 points


    An expert that gotĀ 5 achievements.


    An expert who has written 50 answers of more than 400 characters.


    An expert whose answer gotĀ voted for 20 times.

  • Expert
  • 171 Answers
Re: Java gui code

Okay. I'll try and explain what this code is doing for you.

First thing you need to understand is your "gui" is what is known as "Standard Out" and "Standard In", this is how Java outputs text to the console, and how it inputs text written by a user from the console. A function like "System.out.println([some string])" will print out that line to the console.

Second thing is the idea of classes. A class can be thought of like a code "blueprint". You write the blueprint, and then you create individual "objects" based on that class in your code. While each object is independant, and has it's own properties, functions, and its own spot in memory, it is based on the same code.

If I made a class called, say, "Shape", and I gave it two properties, "Size" and "Color", then I could in my program make lots of different "Shape" objects and they would each have their own independant sizes and colors. Yet, if I made a function in the class called "Draw" which magically drew it on the screen in the appropriate size and color that I had specified, I could call "Draw" for each object, and each shape would be drawn on teh screen. The same code is being executed for each shape, but with different values for the size and color, since each shape object is its own unique entity with its own size and color.

The whole point of classes is so that you don't rewrite code. If you needed to have 1000 shapes on the screen, it would be silly to write the drawing code out 1000 times, one for each shape. It's much easier to write a single blueprint that applies to all shapes.

That said, your code above has a number of "classes" at work.

The "Products" class is a class which stores information about a "Product". In your case, this info looks like it is the name of the product, it's price, how many are in stock etc.

The "Inventory" class is a class who's purpose it is to keep track of an array of products. There are a bunch of functions inside this class. I'll describe what they do below

The buildinventory() function appears to build up a list of products for use in the inventory. Notice how it calls the line:

product[0] = new Product(1, "socket set", 3, 19.99);
filling up the "product" array? Each call there created a new "Product" object which was based on the "Product" class. So each object has it's own name, inventory number, stock, price etc, but they all have the same set of functions to do things like, say, get the price of the product (the getPrice() function), or get the name of the product (the getName() function). It's the same function, as defined in the Product class, but when called on a different product object, it will return different results. This works well for an inventory program.

The "DisplayInventory" function loops through all of the products that the inventory class has created, and outputs information about each one to the screen. By the looks of it it is also keeping track of the total value of the inventory, in a variable called "totalPrice". Every time it loops through a product, it adds the product's price onto the total. The idea is once that while loop has finished, that variable will hold the final "total" of the inventory. After the loop has finished you can do a System.out.println() and use that variable to display the final total. This is probably part of what your project is asking you to do.

The "SortInventoryByName" function simply sorts the list of products that the Inventory class has created based on the name of the product. It does this using a build in Java class that you don't need to worry about.

You'll notice a function that is just called "Inventory()" near the top. This is called the "constructor". When you create a class, the class sometimes has to do a bit of initialization work before it is usable. This function is called as soon as the class is instantiated as an object somewhere in your code. So in this case, when you eventually create an inventory object, that function will be called and the inventory will be built (by calling buildInventory()), sorted (by calling sortInventoryByName()), and displayed (by calling displayInventory()).


So it looks like you have this great Inventory class that does everything you need it to do. So now what? It still doesn't do anything! The reason is because it itself is just a class. It's a BLUEPRINT for an inventory. You need a Java program to USE the inventory class to actually do something useful.

You need to make a java program with a sub main() in it (just like every program you've ever made in this class). And inside there initialize a new inventory object, just like how the inventory object made product objects.
Inventory myInventory = new Inventory();

Remember the Inventory() constructor! It will get called here, and the inventory class will go ahead and build an inventory, sort it, and display it on the screen.

So hopefully this will help you to understand your class a bit better. Again, real programmers don't just take classes and use them, they need to understand how they work. It's true that occasionally you can find something that does what you need it to do, and you don't need to "Reinvent" the wheel, but if you are unable to understand what it is doing, you can never modify it or tweak it, and if your problem isn't perfectly solved using the class, the class is useless to you. Programmers write their own modules and re-use them as they see fit, and occasionally they use someone else's code as a template. This is why you are learning how to modify an existing class.

Posted on Jan 26, 2008

Add Your Answer

0 characters

Uploading: 0%


Complete. Click "Add" to insert your video. Add


3 Points

Related Questions:

2 Answers

What is strings in java?

Handling character strings in Java is supported through two final
classes: Java String and StringBuffer. The String class implements immutable character strings, which are read-only once the string has been created and initialized, whereas the StringBuffer class implements dynamic character strings. All string literals in Java programs, are implemented as instances of String class. Strings in Java are 16-bit Unicode.

Nov 21, 2012 | Sun Java Programming Language (cdj-275)

1 Answer

Java Vitual Machine Launcher prompts this message "Could not find the main class. Program will exit" How can I solve this problem? I need urgent assistance.

You need to compile the .java file you have, and run the .class file that is produced.
Open up command prompt, go to your working directory. If your file was called In command prompt type "javac". Now, in the working directory, you will see a new file, Hello.class. Run that file by "java Hello.class". Hope this helps!
Thanks for using FixYa

Aug 20, 2011 | Sun Java Programming Language (cdj-275)

1 Answer

If the visual basic have its toolbox, what about the Java?

there is no toolbox for sun java programming for it is not a gui language.
if u want java with toolbox,"visual java" is availale on the net, which is pretty much similar to visual basic,the only difference being the language used in the former is "java" and that which used in the latter is "basic".

Jun 23, 2011 | Sun Java Programming Language (cdj-275)

1 Answer

I am doing research on PC vs Mac for unix programming. What are the differences? Which Platform would be preferred?

That was a rather open question.

As long as the language is standards-based, you should have no problem using the language on any platform where it's certified.

That said, anything related to utilising specific operating-system related features such as GUI are much different, except in Java. Since Java is the same output regardless of platform (but version-specific), you can run it on any platform with the correct version (usually the same or newer) Java Virtual Machine. As mentioned, Java programming is the same on almost any platform. Traditionally, Macs lagged way behind Windows as far as Java releases are concerned, but the lag a lot less now (a few months, sometimes less). Macs also are much more Java-friendly.

Non-Java GUI programming is, as mentioned, much different. Honestly, enough tools exist on both platforms to make GUI programming similar in experience, though much different in actual code.

C, C++ both program the same on either platform (again, GUI code will be platform-specific).

A giant plus to Mac programming is that you have access to all the UNIX-type stuff in a way which is a part of the OS, as opposed to on a PC, where it all needs to be added on. I love the Terminal app on OS X - nothing like it on PCs.

Finally, if you're backed by someone with money, the tools on PCs are a bit better than what OS X has to offer, but not substantially.

May 11, 2011 | InstallShield 10.5 Premier...

1 Answer

Hey plz help me java better ya .net ya oracle??

10 Reasons Why dotNET Is Better Than Java

This post is prompted by a post by SANDIP when he said something like "Java Is Dead" and some people were like attacking him for expressing his view.

I've been a fan of the Java technology for quite some time now. I remember literally copying a Java book when I didn't have the money to buy it.

But one thing I discovered with technology is that it's not always about love but it's more about power.
The bitter truth is when I compare java technology with CLI (popularly known as .NET), I see that Java is less powerfull in many areas.

I'll be using the words CLI and .NET interchangeably refferring to any implementation of the CLI (.NET, Mono or dotGnu)
This are some of my points.

1- In .NET you have a choice of languages to code with (C#,VB.NET, Java,Boo,Python e.t.c), producing the same type of compiled code but in Java one is limited to the java language. One may argue that jython is an alternative, but even the creator of jython who later created it's .NET version called IronPython admitted that .NET is a more powerful technology.

2, NET prgrams run at native speed while java is interpreted which makes java slower.Although java has Just In Time compilation but it stills run slower. With .NET you are not limited to JIT but have the option AOT or ahead of time compilation if you want to eliminate startup delays.

3. Calling native code in java is not a very clean process. You need to generate some stub files which makes the whole process cumbersome and dirty. In .NET you just declare that you are calling a native function from a specified library and just start calling it.

4. .NET languages are richer than Java. They have object oriented feature that are absent in java e.g properties,delegates,generics.

5. Java GUI programs look alien on the host operating system. Even if you use the OS's theme you still notice that the java widgets look out of place.

6. .NET in the form of Mono has brought a whole revolution on the linux desktop in form of great applications like beagle, tomboy, diva, iFolder, banshee e.t.c. This is something that java has failed to do despite the fact that it's been there long
before .NET

7. Many programs that would have been difficult to develop with java have been developed with .NET things like compilers (Mono's C# and VB.NET) 3D game engines (unity game engine) e.t.c

8. The CLI is an open standard maintained by an independent standards organization (E.C.M.A) while java is still governed by SUN microsystems.Even though java has recently been open-sourced, it's future will still be highly influenced by SUN.

9. You can code on the .NET platform using Java but you cannot code on Java platform using any of the .NET languages.

10. Using Mono's IKVM to call java code from .NET or convert java classes to .NET assemblies. This is so efficient that large java programs like the eclipse I.D.E have been runned on .NET.

This is not an exhaustive list but I believe 10 points are enough to send the message.

One more thing, feel free to criticize my views or even flame me.

----YOUR CHIRAG------

Jul 17, 2010 | Sun Java Programming Language (cdj-275)

4 Answers

Java is not working

Hello there :
first you are going to want to ensure that your your current version of java is installed correctly and is not corrupted and if it needs to be reinstalled
download the latest java version and reinstall this .
this should be able to take care of the problem that you are having ok?
best regards michael

Aug 23, 2009 | ArcMedia JavaScript Source Code 3000 Pro...

1 Answer

Code for method parseInt() in java

parseInt(java.lang.String, int)

This code takes two arguments, a string constructor from the Java language and an integer. Its purpose is to parse (meaning de-construct or disassemble into component parts) one data type into another.

Here is an example of a Java program to specify and print out the dimensions of a rectangle, taken from the page

class Rectangle{
int length, breadth;
void show(int x, int y){
length = x;
breadth = y;
int calculate(){
return(length * breadth);
public class EnterValuesFromKeyboard{
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);, b);
System.out.println(" you have entered these values : " + a + " and " + b);
int area = rectangle.calculate();
System.out.println(" area of a rectange is : " + area);

Apr 09, 2009 | Sun Java Programming Language (cdj-275)

4 Answers

Can anyone tell how run java program in cmd prompt???


You need to type:

If it's a program.class

java program

or of it's a program.jar

java -jar program.jar


Apr 03, 2009 | Sun Java Programming Language (cdj-275)

1 Answer

What is swing components?

Swing is java library that provides basic GUI controls [also called widgets like buttons, checkboxes, labels, menus, etc.. You can use it to create a interactive application with nice interface.

More info here

Mar 10, 2009 | Sun Compilers & Programming Tools

1 Answer

Unable to View Java Applet

"load: class JFI.class not found."
Based on the console output, it means that it cannot find the java applet class. In other words, the applet is missing on the server side. There is nothing that you can do about it, unless you know the person who created the applet. Good luck :)

Dec 23, 2008 | ArcMedia JavaScript Source Code 3000 Pro...

Not finding what you are looking for?
Compilers & Programming Tools Logo

Related Topics:

384 people viewed this question

Ask a Question

Usually answered in minutes!

Top Compilers & Programming Tools Experts

Robin Hill
Robin Hill

Level 3 Expert

1246 Answers


Level 3 Expert

18258 Answers


Level 3 Expert

733 Answers

Are you a Compiler and Programming Tool Expert? Answer questions, earn points and help others

Answer questions

Manuals & User Guides