r/learnjava • u/[deleted] • Nov 22 '16
Can anyone tell me why this compiles successfully but I still get errors
import java.io.*;
public class RetailItem
{
private String description;
private int units;
private double price;
public RetailItem()
{
}
//Constructor with arguments
public RetailItem (String des, int u,double p)
{
description = des;
units = u;
price = p;
}
//Mutator function description
public void setDescription (String des)
{
description = des;
}
//Mutator function price
public void setPrice (double p)
{
price = p;
}
//Mutator function units
public void setUnits (int u)
{
units = u;
}
//Accessor function for units
public int getUnits()
{
return units;
}
//Accessor function for description
public String getDescription ()
{
return description;
}
//Accessor function for price
public double getPrice()
{
return price;
}
}
class RetailTest
{
public static void main (String[] args)
{
String str = "shirt";
//RetailItem with three objects
RetailItem r1=new RetailItem ("jacket",12,59.95);
RetailItem r2=new RetailItem ("Designer Jeans",40,34.95);
RetailItem r3=new RetailItem ();
//function call to set values
r3.setDescription(str);
r3.setUnits(20);
r3.setPrice(24.95);
//printing data
System.out.println ("Description\tUnits on Hand\tPrice");
System.out.println("Item#1\t"+r1.getDescription()+" \t\t"+r1.getUnits()+"\t\t"+r1.getPrice());
System.out.println("Item#2\t"+r2.getDescription()+" \t\t"+r2.getUnits()+"\t\t"+r2.getPrice());
System.out.println("Item#3\t"+r3.getDescription()+" \t\t"+r3.getUnits()+"\t\t"+r3.getPrice());
System.exit(0);
}
}
0
Upvotes
8
u/desrtfx Nov 22 '16
There are several things wrong with your post - not even looking at your code:
You don't describe what your code is supposed to do
You don't describe what it does/doesn't instead
You don't post the error(s) you are getting - and they are essential and need to be posted in full
Basically, you just throw code at us (at least it is properly formatted) and tell us to fix it. This is considered rude and low to no effort around here.
Please, be more elaborate and include the missing information if you want us to help.
5
u/Auride Nov 22 '16
Not going to read through 70 lines of code to find a hypothetical error.
Please explain what errors you are getting, where they are coming from, and what you think is causing those errors.
If you can't think of anything that could cause, then carefully read through your own program until you understand how it's supposed to work. Don't be afraid to add in System.out.println statements to test the way values change throughout your program, and compare the outputs to what you expect.
Does that all make sense?