What's new

Java Programming - Can anyone help me?

Im taking a java class as part of my game testing degree, but the only problem is ive never looked at a line of code before in my life, and the class is Online. I need major help with just the basics. If you can please help me, it would be greatly appreciated. I spend every day for hours trying to figure it out and cannot and its holding me back in all my other classes etc... I couldnt even pick up castlevania this week because of it. I wont be able to go to this tourny thing in december either if i dont get this figured out first... So please HELP!!!
 
Ill just copy what my professor wrote:

_________

To complete this assignment you need to create 2 Java classes. One is an application class that contains the main method. This is the type of class we have been creating and running since the semester began.

The 2nd class is the definition of the invoice.

Concept:
The Invoice class will define the properties of an invoice (item number, name, quantity, price, total cost). It will also contain 3 set methods to set the item name, quantity, and price. The Invoice class will also have a method to recalculate the total. This method will be called from set quantity and set price. The Invoice class will also contain a displayLine method that displays all the data.

After you create the Invoice class, you compile it (green plus sign) to get rid of the syntax errors. Do not try to run it as it does not have a main method.

Step 2 is to create the application class which contains the main method. This is a separate .java file. The book advises that the class should be named TestInvoice.

_________

I have created the Invoice class already. She told me it was done. It is the TestInvoice class that i am having trouble with. I will add both codes to my next post so you can take a look at it, along with my professors notes (again).
 
public class Invoice
{
private int item;
private int name;
private int quantity;
double price;
double total;
public int getItemName()
{
return name;
}
public void setItemName(int name)
{
name = name;
System.out.print("Item Name: " + name);
}
public double getPrice()
{
return price;

}
public void setItemPrice(double price)
{
price = price;
total = price * quantity;
System.out.print("Price of Item: " + item);
}
public int getItemQuantity()
{
return quantity;

}
public void setItemQuantity(int quantity)
{
quantity = quantity;
total = price * quantity;
System.out.print("Quantity: " + quantity);
}
public static void displayLine(int item, int name, int quantity, double price, double total)
{
System.out.print(item);
System.out.print(name);
System.out.print(quantity);
System.out.print(price);
System.out.print(total);
}
}
 
here is my TestInvoice class (i have no idea what im doing for it):

import javax.swing.JOptionPane;
public class TestInvoice
{
public static void main(String[] args)
{
Invoice shoes = new Invoice();
String itemNumber, name, quantity, price;
itemNumber = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
name = JOptionPane.showInputDialog(null, "Please Enter the Item Name");
quantity = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
price = JOptionPane.showInputDialog(null, "Please Enter the Price");
JOptionPane.showMessageDialog(null, "Your Invoice: " + "\n" +
"Item Number: " + itemNumber + "\n" +
"Item Name: " + name + "\n" +
"Quantity: " + quantity + "\n" +
"Price of Item: " + price + "\n");
}
{
Invoice fruit = new Invoice();
String itemNumber, name, quantity, price;
itemNumber = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
name = JOptionPane.showInputDialog(null, "Please Enter the Item Name");
quantity = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
price = JOptionPane.showInputDialog(null, "Please Enter the Price");
JOptionPane.showMessageDialog(null, "Your Invoice: " + "\n" +
"Item Number: " + itemNumber + "\n" +
"Item Name: " + name + "\n" +
"Quantity: " + quantity + "\n" +
"Price of Item: " + price + "\n");
}
{
Invoice bread = new Invoice();
String itemNumber, name, quantity, price;
itemNumber = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
name = JOptionPane.showInputDialog(null, "Please Enter the Item Name");
quantity = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
price = JOptionPane.showInputDialog(null, "Please Enter the Price");
JOptionPane.showMessageDialog(null, "Your Invoice: " + "\n" +
"Item Number: " + itemNumber + "\n" +
"Item Name: " + name + "\n" +
"Quantity: " + quantity + "\n" +
"Price of Item: " + price + "\n");
}
}



_____

her notes back from the testinvoice are as follows:

Your input statements are fine, but you never call the set methods of the class to get the values into the class. When you put them into the class your set methods will auto calculate total price. The you have a displayLine method that will display everything.

TestInvoice needs to
1) declare variables for itemNumber. name, quantity, price, and 3 objects of the Invoice type.
2) instantiate the 3 objects using the new keyword
3) get values for the 1st invoice then call the set methods of the Invoice class
4) get values for the 2nd invoice then call the set methods on the Invoice class
5) get values for the 3rd invoice then call the set methods of the Invoice class
6) call the displayLine method of the Invoice class 3 times, once for each invoice object
 
Here was the final product for TestInvoice:

public class TestInvoice
{
public static void main(String[] args)
{
int itemNumber;
String name;
int quantity;
double price;
Invoice item1 = new Invoice();
Invoice item2 = new Invoice();
Invoice item3 = new Invoice();
name = "Shoes";
itemNumber = 27;
quantity = 2;
price = 50.25;
item1.setItemName(name);
item1.setItemNumber(itemNumber);
item1.setItemQuantity(quantity);
item1.setItemPrice(price);
item1.displayLine();
name = "Fruit";
itemNumber = 13;
quantity = 5;
price = 7.15;
item2.setItemName(name);
item2.setItemNumber(itemNumber);
item2.setItemQuantity(quantity);
item2.setItemPrice(price);
item2.displayLine();
name = "Bread";
itemNumber = 7;
quantity = 2;
price = 3.00;
item3.setItemName(name);
item3.setItemNumber(itemNumber);
item3.setItemQuantity(quantity);
item3.setItemPrice(price);
item3.displayLine();
}
}


a few things needed to be modified to the Invoice class. Things like adding String instead of int/char for ItemName (thanks scheiss) as well as a few other minor things.

One of the things she noted was to add spacing between the output. I added "\n" between each line and that worked, but my only problem is how do i seperate each Invoice after it is displayed? Im sure that is an easy question.

This is what the output looks like:

Item Number: 27
Item Name: Shoes
Quantity: 2
Price of Item: 50.25
Total Cost: 100.5
Item Number: 13
Item Name: Fruit
Quantity: 5
Price of Item: 7.15
Total Cost: 35.75
Item Number: 7
Item Name: Bread
Quantity: 2
Price of Item: 3.0

I just want to be able to add a space in between each Invoice. Its calling the displayLine() method from the Invoice class so i'd imagine its some sort of modification there? Its a simple answer, i know. Im just not very good at this stuff yet.
 
well, it looks like i wasnt done. I just got comments back from the professor on it. Heres what she says:

______

"Hi Jason.

You're almost there... The assignment says you are to prompt for the values. So remove
name = "Shoes";
itemNumber = 27;
quantity = 2;
price = 50.25;

and write code using System.in or JOptionPane to prompt for the values."

_______

When i replace it, i end up getting an error messages :

TestInvoice.java:13: incompatible types
found : java.lang.String
required: int
itemNumber = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
^
TestInvoice.java:15: incompatible types
found : java.lang.String
required: int
quantity = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
^
TestInvoice.java:16: incompatible types
found : java.lang.String
required: double
price = JOptionPane.showInputDialog(null, "Please Enter the Price");
^
3 errors

EDIT: I knew you mentioned something in your post that might be the answer:

setItemPrice(price) = JOptionPane.showInputDialog(null, "Please Enter the Price");

I apologize for not using the Code tag. Im not sure how to use it yet. My bad again.
 
"
Code:
"
public class TestInvoice
{
public static void main(String[] args)
{
int itemNumber;
String name;	
int quantity;
double price;
Invoice item1 = new Invoice();
name = '\u0000';
itemNumber = 0;
quantity =  0;
price = 0;
itemNumber = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
name = JOptionPane.showInputDialog(null, "Please Enter the Item Name");
quantity = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
price = JOptionPane.showInputDialog(null, "Please Enter the Price");
item1.setItemName(name);
item1.setItemNumber(itemNumber);
item1.setItemQuantity(quantity);
item1.setItemPrice(price);
item1.displayLine();
}
"
"

like that?

Ed Boon, i hope you're reading this!
 
3 errors:

TestInvoice.java:14: incompatible types
found : java.lang.String
required: int
itemNumber = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
^
TestInvoice.java:16: incompatible types
found : java.lang.String
required: int
quantity = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
^
TestInvoice.java:17: incompatible types
found : java.lang.String
required: double
price = JOptionPane.showInputDialog(null, "Please Enter the Price");
^
3 errors

JGrasp.

I think i need to add Double.parseDouble and Integer.parseInteger somewhere don't I?
 
Holy shit, it worked.

Code:
import javax.swing.JOptionPane;
public class TestInvoice
{
public static void main(String[] args)
{
int itemNumber;
String name;	
int quantity;
double price;
Invoice item1 = new Invoice();
String tempString = "";
tempString = JOptionPane.showInputDialog(null, "Please Enter the Item Number");
itemNumber = Integer.parseInt(tempString);
tempString = JOptionPane.showInputDialog(null, "Please Enter the Quantity");
quantity = Integer.parseInt(tempString);
tempString = JOptionPane.showInputDialog(null, "Please Enter the Price");
price = Double.parseDouble(tempString);
name = JOptionPane.showInputDialog(null, "Please Enter the Item Name");
item1.setItemName(name);
item1.setItemNumber(itemNumber);
item1.setItemQuantity(quantity);
item1.setItemPrice(price);
item1.displayLine();
}
}
Any other suggestions, Scheisse? Thanks a lot man!
BTW, adding the = 0; made all the the calculations come out to 0
 
I believe i followed your directions as so:

Code:
public void displayLine
{ 
System.out.print("\n")
System.out.print(item + "\n"); 
System.out.print(name + "\n"); 
System.out.print(quantity + "\n"); 
System.out.print(price + "\n"); 
System.out.print(total + "\n"); 
System.out.print("\n")
} 
}

My last question, actually, if you can just write it for me, that would be great, is how do i add the other 2 invoices? I know its the same exact code. But yea, if you can just help me with that. Thats why i only did item1 for now. Wasnt 100% sure about the other 2 (sorry for the scrubby questions).
 
wow.....

Ok, no more questions. :)

BTW, she did ask for the display lines at the very end, so good call, Scheiss. Rather beastly post that was. Thank you so much, dude.
 
It actually has a lot to do with how you relay the information. Instead of just giving me all this terminology and expect me to understand, you talk to me about it like a regular person and make it easier to understand (as if it were real life, almost).

I cannot thank you enough. Im a dumbass for not making this thread weeks ago, because thats how long that invoice took, then add all the other assignments since the semester started, then add the assignments i still have to do. Im a week behind on them, so if i ask for help again, it will be on that. I just need to catch up.

Failed my 2D game programming exam this morning... :( My first failing grade since starting school a year ago ( I have a 4.00 GPA btw). All i care about is passing these programming classes, but yea, that invoice completely screwed me up in all my classes. Should of made this thread sooner.

Thanks again and again and again SN.
 
Great post as usual, SN.

My course outline is the same:
1) 2D
2) 3D
3) 3D Lab

just started the 2D, so im just learning action script. ill be creating a matching game i think down the road...

the exam was way too hard. 35 of the 50 questions on the multiple choice were code. some had 2 answers... one student walked out when he saw that LOL. unbelieavble...

No, i dont like my school. I have no choice because of money and cant drive. The Invoice thing was fucking stupid. Asking the professor for help for weeks = not cool.

Moving right along though. This next chapter isnt nearly as hard, but the "Invoice" type of assignment i have to do for it looks really hard...

I noticed when people help you, they dont flat out give you the answers, they "guide" you. You did the same, but the way you spoke made it easy for anyone to understand.
 
Ah ok, yeah I know actionscript better than any other language sadly. I wish I had been learning C# or something instead but it was just easier to start with. It certainly is a great language, but Flash is limited right now. They are planning on beefing up a Flash player for game development soon though, so that could be pretty damned interesting.

I had Java 1, Java 2, and Data Structures using Java lol. Java 2 and DS were back to back with the same professor too, it felt like the school year from hell. I already have a degree for programming and database processing but there is also a reason I decided to not do that for the rest of my life... it is fucking BORING!

Yeah I'm fine with being guided in the right direction. But when you ask someone a question about something that you couldn't figure out it 2 days and they tell you to google it I feel like killing somebody, especially if there is a deadline coming up.

I can't wait to start my 2D programming class. My school wouldn't carry an actionscript class because Adobe was trying to get them to buy a copy of Flash for every machine that they wanted to install it on. I still think they should have done it, or used Flex instead for how much we pay. I'm not sure what we'll even be using.
Well, i took a multimedia class last spring and learned Flash CS3, and now they are using CS5 on their computers and shit has been an issue to adjust to....

The exam i failed... Teacher curved it 30 points, then apoligized to the whole class for making it too hard. so i ended up with a 90 :)

Im ready to go over this weeks stuff with you. I tried my best, but this OTHER teacher just doesnt get it either... Making the shit way too hard on us.

Im real excited for you, SN. I know how long youve been at it and where you stand now. Couldnt have happened to a nicer guy. All that good karma will pay off. Here's a root for you.

Next post i will lace up my assignments. Basically what i have to do on the first half is correct syntax errors. The other half im actually just gonna post the assignment word for word from the book since im not even sure how to class it, etc.
 
First Assignment:

Code:
public class FixDebugFour1
// This program assigns values to two variables
// and performs mathematical operations with them
{
 public static void main(String args[])
{
  x = 5;
  y = 8;
  System.out.println("adding " + x + y );
 {
   z = 19;
   System.out.println("subtracting "  + (z - y));
 }
   System.out.println("dividing " +  z/x );
   System.out.println("multiplying " + x ** z);
 }
}

My corrections:

Code:
public class FixDebugFour1
{
 public static void main(String args[])
{
int x;
int y;
int x
x = 5;
y = 8;
z = 19;
System.out.println("adding " + x + y );
System.out.println("subtracting "  + (z - y));
System.out.println("dividing " +  z/x );
System.out.println("multiplying " + x * z);
}
}

Second Assignment:

Code:
// Some circle statistics
public class DebugFour2
{
 public static void main(String args[])
{
dooble radius = 12.6;
System.out.println("Circle statistics");
dooble area = java.lang.pi * radius * radius;
System.out.println("area is " + area);
dooble diameter = 2 * radius;
System.out.println("diameter is " + diametter);
}
}

My corrections:

Code:
import java.util.*;
import java.lang.*;
public class FixDebugFour2
{
public static void main(String args[])
{
double radius = 12.6;
double area;
double roundArea;
System.out.println("Circle statistics");
area = Math.PI * radius * radius;
System.out.println("area is " + area);
double diameter = 2 * radius;
System.out.println("diameter is " + diameter);
}
}

Im actually going to stop it there for now and make a new post with the next 2 since im really struggling with them.
 
This next one is somewhat related to Invoice. I cant seem to figure out what it is im missing. Both files run, but the values all come out to zero. Need major help on this one:

(Both files go together like Invoice)

FixDebugBox

Code:
public class FixDebugBox
{
private int box;
private double width;
private double length;
private double height;
private double volume;
public FixDebugBox()
{
length = 1;
width = 1;
height = 1;
}
public FixDebugBox(double width, double length, double height)
{
width = width;
length = length;
height = height;
}
public void showData()
{
System.out.println("Width: "  + width + "  Length: " +
length + "  Height: " + height);
}
public double getVolume()
{ 
double vol = length * width * height;
	  
return volume;
}
public void showVolume()
{
double vol = getVolume();
}
public void displayLine()
{
System.out.print("\n");
System.out.print("The dimensions of the box are "); 
System.out.print("The volume of box is ");            
System.out.print("\n"); 
} 
}



FixDebugFour3

Code:
public class FixDebugFour3
// This class uses a FixDebugBox class to instantiate two Box objects
{
  public static void main(String[] args)
  {
   int width = 12;
   int length = 10;
   int height = 8;
   int width2 = 10;
   int length2 = 4;
   int height2 = 20;
   FixDebugBox box1 = new FixDebugBox(width, length, height);
   FixDebugBox box2 = new FixDebugBox(width2, length2, height2);
	
   //box1
   box1.showData();
   box1.showVolume();
   box1.displayLine();
   
   // box2
   box2.showData();
   box2.showVolume();
   box2.displayLine();
  }
}

When i run it, this is the output:

"Width: 0.0 Length: 0.0 Height: 0.0

The dimensions of the box are The volume of box is
Width: 0.0 Length: 0.0 Height: 0.0

The dimensions of the box are The volume of box is"


Not sure whats going on.
 
i forgot to post the 4th assignment. After we get through these, we will move on the second half.

Code:
public class FixDebugFour4
// This class discounts prices by 10%
{
  public static void main(String args[])
  {
	int price = 100;
	double price2 = 100.00;
	tenPercentOff(price);
	tenPercentOff(price2);
  }
  public static void tenPercentOff(int p)
  {
	double newPrice = price * .90;
	System.out.println("Ten percent off";
	System.out.println("New price is " + nwPrice);
  }
  public static void tenPercntOff(double price)
  {
	double price = p * .90;
	System.out.println("Ten percent off");
	System.out.println("New price is " + newPrice);
  }
}

My corrections

Code:
public class FixDebugFour4
// This class discounts prices by 10%
{
  public static void main(String args[])
  {
	double price = 100.00;
	double rate = 0.10;
	double discount = price * rate;
	double newprice = price - discount;
	System.out.print("Ten percent off is " + discount + "\n");
	System.out.print("New price is " + newprice);
  }
}
esssentially, FixDebugBox and FixDebugFour3 in my previous post are what is causing me problems. I believe all im missing is some call methods and a second displayLine. The values are all out of whack too, that part i am completly lost at ;(

I will post the second half tomorrow.
 
1) endlines? What are those?

2) OK

3) That sounds reasonable. I will look over it tomorrow. Too tired from yesterday.

4) That was my mistake. One of the things they teach us in 2D game programming is to take smaller steps when you dont understand something. All i wanted to do was the first one just to make sure it was right. The second one is just a matter of copy and paste. Thanks for the reassurance. Thats a big reason why i come to you regardless if i know/dont know.

I will get the next part up tonight though for you to look at. Thanks again, dude.
 
Here is the second half (word for word from book) :

1. Dice are used in many games. One die can be thrown to randomly show a value from 1 to 6. Design a Die class that can hold an integer data field for a value (from 1 to 6). Include a constructor that randomly assigns a value to a die object.

Also include a method in the class to return a die's value. Save the class as Die.java

Write an application that randomly "throws" two dice and displays their values. Save as TwoDice.java

______________


2. Using the Die class, write an application that randomly "throws" fice dice for the computer and five dice for the player. Display the values and then, by observing the results decide who wins based on the following:

- five of a kind
- four of a kind
- three of a kind
-a pair

Save as FiveDice.java


___

:(
 
How about this for the Die Class:

Code:
import java.util.*;
import java.lang.*;
// blah
public class Die
{
int dieOne;
int dieTwo;
int dieThree;
int dieFour;
int dieFive;
int dieSix;
int dieValue;
int value;
public int Die()
{
dieOne = 1;
dieTwo = 2;
dieThree = 3;
dieFour = 4;
dieFive = 5;
dieSix = 6;
dieValue = ((int) (Math.random() * 100) % 6 + 
1);

return value;
}
}
sorry i am just completly lost on this assignment. i need more help if you dont mind.

EDIT: I figured i'd throw this in there. its the teachers notes:

1 - part 1) Create a Die class. It has a constructor that uses the formula provided to set a value for the data member dieValue (or whatever you choose to call it). Code an accessor method to return the value.

1 - part 2) Create TwoDice class. Instantiate 2 objects of the Die class. Display the values of the 2 dice.

2) Do the same but instantiate 10 objects (or use 5 objects twice).
 
I think youre right about the Die class, all it is is that one code.

Keep in mind that it states that PLayer 1 throws the dice 5 times and the CPU throws it 5 times. ALl im doing is observing the results. nothing more. So i think that means to create a dieOne1 etc for the second player.

Really, its going to be hard to do anything till you get back, but i will at least try.
 
Die Class:

Code:
import java.util.*;
import java.lang.*;
// blah
public class Die
{
int dieValue;
public int Die()
{
dieValue = ((int) (Math.random() * 100) % 6 + 
1);

return dieValue;
}
}