1 Replies - 2116 Views - Last Post: 22 August 2008 - 08:54 PM Rate Topic: -----

#1 colec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-August 08

Compiler (NetBeans 6.1) issue or code

Post icon  Posted 22 August 2008 - 07:36 PM

I am having an issue with the following code. I get a compiler error in the inventory file. Can someone take a look and see where I might be going wrong? I also am having a hard time with the command super. Looks like Netbeans does not acknowledge the command. Here are the codes (4 total) broken up by _________________________________

import java.util.*;

public class Inventory {
public static void main(String[] args) {

private CD[] list;

// constructor
public Inventory(int size) {
list = new CD[size];

}

// complete value
public double totalValue() {
double val = 0.0;
for (int i = 0; i < list.length; i++) {
val += list[i].value();
}
return val;
}

// add an item
public void add(CD d, int p) {
list[p] = d;
}

// get an item
public CD get(int i) {
return list[i];
}

// sort by name
public void sort() {
// bubble sort
int n = list.length;
for (int search = 1; search < n; search++) {
for (int i = 0; i < n-search; i++) {
if (list[i].getName().compareToIgnoreCase(list[i+1].getName()) > 0) {
// swap
CD temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
}
}
}
}
}

_____________________________________________

// extended class
public class CD extends Product {
public static void main(String[] args) {

}
// rating of the CD
private String rating;

// constructor
public CD(int item, String name, int units, double price, String rating) {
super(item,name,units, price);
this.rating = rating;
}

// getter
public String getRating() {
return rating;
}


// setter
public void setRating(String rating) {
this.rating = rating;
}

// total value with the 5% fee
public double value() {
return super.value()*1.05;
}

public double fee() {
return super.value()*0.05;
}

public double valueNoFee() {
return super.value();
}

// string
public String toString() {
return String.format(" %03d %-10s %3d $%6.2f $%7.2f %8s",
super.getNumber(), super.getName(), super.getUnits(), super.getPrice(), value(), getRating());
}
}
_____________________________________________________

public class Inventory1 {

public static void main(String[] args) {

System.out.println("\nCraig's CD Inventory\n");
// create 3 CDs
CD p1 = new CD(1, "Blues", 3, 15.49,"PG");
CD p2 = new CD(14, "Rock", 10, 10.79,"R");
CD p3 = new CD(3, "Pop", 1, 23.99,"PG-13");

// put them in an inventory object
Inventory inv = new Inventory(3);
inv.add(p1,0);
inv.add(p2,1);
inv.add(p3,2);

// sort
inv.sort();

// output the CDs using proper formatting, with a header row
// the columns are spaced using tabs to look nice
System.out.println("Number\tName\tUnits\tPrice\tValue\tRating");

// write out the products
System.out.println(inv.get(0));
System.out.println(inv.get(1));
System.out.println(inv.get(2));

System.out.println();
System.out.println("Total value: " + inv.totalValue());
}

}
_______________________________________________-

public class Product {
public static void main(String[] args) {

}
private int number;
private String name;
private int units;
private double price;

// constructor
public Product(int number, String name, int units, double price) {
this.number = number;
this.name = name;
this.units = units;
this.price = price;
}

// value of this DVD
double value() {
return price * units;
}

// getters and setters

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the units
*/
public int getUnits() {
return units;
}

/**
* @param units the units to set
*/
public void setUnits(int units) {
this.units = units;
}

/**
* @return the price
*/
public double getPrice() {
return price;
}

/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}

/**
* @return the number
*/
public int getNumber() {
return number;
}

/**
* @param number the number to set
*/
public void setNumber(int number) {
this.number = number;
}

public String toString() {
return String.format(" %03d %-10s %3d $%6.2f $%7.2f",
number, name, units, price, value());
}

}

Any feedback would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Compiler (NetBeans 6.1) issue or code

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5121
  • View blog
  • Posts: 13,788
  • Joined: 18-April 07

Re: Compiler (NetBeans 6.1) issue or code

Posted 22 August 2008 - 08:54 PM

The problem appears to be that you either forgot to close your main function before doing your constructor or are trying to define your constructor in main. Remember that the constructor is a separate function from main. So you need to close it off before you can declare the constructor...

import java.util.*;

public class Inventory {
	public static void main(String[] args) { 
		// Do stuff here in main, notice the closing curly brace
	}

	private CD[] list;

	// constructor
	public Inventory(int size) {
		list = new CD[size];
	}

	// complete value
	public double totalValue() {
		double val = 0.0;
		for (int i = 0; i < list.length; i++) {
			val += list[i].value();
		}
		return val;
	}

	// add an item
	public void add(CD d, int p) {
		list[p] = d;
	}

	// get an item
	public CD get(int i) {
		return list[i];
	}

	// sort by name
	public void sort() {
		// bubble sort
		int n = list.length;
		for (int search = 1; search < n; search++) {
			for (int i = 0; i < n-search; i++) {
				if (list[i].getName().compareToIgnoreCase(list[i+1].getName()) > 0) {
					// swap
					CD temp = list[i];
					list[i] = list[i+1];
					list[i+1] = temp;
				}
			}
		}
	}
}



Notice that we closed off the main method prior to defining the constructor. As for using super, super is used in a class to call the base class' version of a method. When used in the constructor (and it must be the first line in the constructor) it is simply calling the constructor of the base class (which initializes the base class) so that the current child class can then inherit from it.

Hope that makes sense. Enjoy!

"At DIC we be super() using code ninjas... yeah perhaps superbad!" :snap:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1