COP2800 -

-


Hi Dwain, i was putting together this code with hierarchy/inheritance and keep getting compiler errors as shown below.

Any idea on how to get the code printed? Your help is truly appreciated.

 

/*compiler error:
Line: 29 - (package1 new package)
non-static variable this cannot be referenced from a static context
Line: 47 - (package2 new twodaypackage)
non-static variable this cannot be referenced from a static context
Line: 65 - (package3 new overnightpackage)
non-static variable this cannot be referenced from a static context*/

/*TestPackage.java
In this program we're creating an inheritance hierarchy to represent 
various types of packages and their costs. Package is the base class, 
TwoDayPackage and OvernightPackage are inherited from this base class.
These classes use the packages' weight in ounces and shipping fee to 
determine the total cost of the shipping. TwoDayPackage and 
OvernightPackage include extra fees that add to the total cost.*/

import java.text.NumberFormat;
class TestPackage{
public static void main(String[] args) {

//formatter I'm using to display costs in dollars.
 NumberFormat dollar = NumberFormat.getCurrencyInstance();

//describing the number used for my rates.
 System.out.println("Item's weight 5 oz, shipping fee 0.75/oz," +
   "the flat fee $3, the overnight rate $1 per oz.");
   
//creates an object called package1 out of the base class Package
 Package package1 = new Package( 
 "Lou Brown",   "1 Main St", "Boston",   "MA", "11111", 
  "Mary Smith", "7 Elm St",  "New York", "NY", "22222",
  5, .75);

//uses calculateCost method from the base class to get total cost 
 package1.calculateCost(package1.weight, package1.costPerOunce);

//displays parameters of the object and total value from calculateCost method.
 System.out.println("Package 1: Sender: " +package1.senderName + 
   " "  + package1.senderAddress+ " " + package1.senderCity+", " +
   package1.senderState +" " + package1.senderZIP);
 System.out.println(" Recipient: " +package1.recipientName + " "
   + package1.recipientAddress+ " " + package1.recipientCity+", " 
   +package1.recipientState+ " " + package1.recipientZIP + " Cost: " 
   + dollar.format(package1.totalCost) +" ");         

//creates an object called package2 out of the inherited class TwoDayPackage
 TwoDayPackage package2 = new TwoDayPackage(
   "Lisa Klein", "5 Broadway", "Somerville", "MA", "33333",
   "Bob George", "21 Pine Rd", "Cambridge",  "MA", "44444",
   5, .75, 3);

//overridden calculateCost method includes flat fee from TwoDayPackage class.
 package2.calculateCost(package2.weight, package2.costPerOunce);

//displays this object.
 System.out.println("Package 2: Sender: " +package2.senderName + " "
   + package2.senderAddress+ " " + package2.senderCity+", " + 
   package2.senderState + " " + package2.senderZIP);
 System.out.println(" Recipient: " +package2.recipi


Log in to post an answer

Answer by lucio on November 14, 2016 8:39 AM

i got it now, it's just a matter of being more specific with the method totalCost invoking those parameters at print statment:

The totalCost method was replaced by getCost defined locally at the class that will use it.

After declaring the method, change the print statement accordingly:

   System.out.println("  ...   + " Cost: " + package1.getCost() + " "); // removed package1.totalCost

Insted of:

   System.out.println("Package 1: Sender: " +  ...  + " Cost: "  + dollar.format(package1.totalCost) +" "); 

The same way goes for the the other packages 2 and 3.

Each one will have a method to calculate cost on its own.

package2 can have the getFlatFee and  package3, the getOvernightFee.

 

 


COP2800 -

-


Hi Dwain, i was putting together this code with hierarchy/inheritance and keep getting compiler errors as shown below.

Any idea on how to get the code printed? Your help is truly appreciated.

 

/*compiler error:
Line: 29 - (package1 new package)
non-static variable this cannot be referenced from a static context
Line: 47 - (package2 new twodaypackage)
non-static variable this cannot be referenced from a static context
Line: 65 - (package3 new overnightpackage)
non-static variable this cannot be referenced from a static context*/

/*TestPackage.java
In this program we're creating an inheritance hierarchy to represent 
various types of packages and their costs. Package is the base class, 
TwoDayPackage and OvernightPackage are inherited from this base class.
These classes use the packages' weight in ounces and shipping fee to 
determine the total cost of the shipping. TwoDayPackage and 
OvernightPackage include extra fees that add to the total cost.*/

import java.text.NumberFormat;
class TestPackage{
public static void main(String[] args) {

//formatter I'm using to display costs in dollars.
 NumberFormat dollar = NumberFormat.getCurrencyInstance();

//describing the number used for my rates.
 System.out.println("Item's weight 5 oz, shipping fee 0.75/oz," +
   "the flat fee $3, the overnight rate $1 per oz.");
   
//creates an object called package1 out of the base class Package
 Package package1 = new Package( 
 "Lou Brown",   "1 Main St", "Boston",   "MA", "11111", 
  "Mary Smith", "7 Elm St",  "New York", "NY", "22222",
  5, .75);

//uses calculateCost method from the base class to get total cost 
 package1.calculateCost(package1.weight, package1.costPerOunce);

//displays parameters of the object and total value from calculateCost method.
 System.out.println("Package 1: Sender: " +package1.senderName + 
   " "  + package1.senderAddress+ " " + package1.senderCity+", " +
   package1.senderState +" " + package1.senderZIP);
 System.out.println(" Recipient: " +package1.recipientName + " "
   + package1.recipientAddress+ " " + package1.recipientCity+", " 
   +package1.recipientState+ " " + package1.recipientZIP + " Cost: " 
   + dollar.format(package1.totalCost) +" ");         

//creates an object called package2 out of the inherited class TwoDayPackage
 TwoDayPackage package2 = new TwoDayPackage(
   "Lisa Klein", "5 Broadway", "Somerville", "MA", "33333",
   "Bob George", "21 Pine Rd", "Cambridge",  "MA", "44444",
   5, .75, 3);

//overridden calculateCost method includes flat fee from TwoDayPackage class.
 package2.calculateCost(package2.weight, package2.costPerOunce);

//displays this object.
 System.out.println("Package 2: Sender: " +package2.senderName + " "
   + package2.senderAddress+ " " + package2.senderCity+", " + 
   package2.senderState + " " + package2.senderZIP);
 System.out.println(" Recipient: " +package2.recipi


Log in to post an answer

MAC2311 -

Extrema - Critical Numbers


How do I find the derivative of y = 3 cosx


Log in to post an answer

COP2800 -

Array - Methods


how to fix the random character code? im getting this output:
Compilation Errors Detected  Line: 31
cannot find symbol
  symbol:   variable RandomCharacter
  location: class CountLettersInArray

Your help is appreciated.

 

public class CountLettersInArray {

// Main method 
public static void main(String[] args) {

// Declare and create an array
char[] chars = createArray();

// Display the array
System.out.println("The lowercase letters are:");
displayArray(chars);

// Count the occurrences of each letter
int[] counts = countLetters(chars);

// Display counts
System.out.println();
System.out.println("The occurrences of each letter are:");
displayCounts(counts);
}

// Create an array of characters 
public static char[] createArray() {

// Declare an array of characters and create it
char[] chars = new char[100];

// Create lowercase letters randomly and assign
// them to the array
for (int i = 0; i < chars.length; i++)
chars[i] = RandomCharacter.getRandomLowerCaseLetter();

// Return the array
return chars;
}

// Display the array of characters 
public static void displayArray(char[] chars) {

// Display the characters in the array 20 on each line
for (int i = 0; i < chars.length; i++) {
if ((i + 1) % 20 == 0)
System.out.println(chars[i]);
else
System.out.print(chars[i] + " ");
}
}

// Count the occurrences of each letter 
public static int[] countLetters(char[] chars) {

// Declare and create an array of 26 int
int[] counts = new int[26];

// For each lowercase letter in the array, count it
for (int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;

return counts;
}

// Display counts 
public static void displayCounts(int[] counts) {
for (int i = 0; i < counts.length; i++) {
if ((i + 1) % 10 == 0)
System.out.println(counts[i] + " " + (char)(i + 'a'));
else
System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
}
}
}


Log in to post an answer

Answer by dwain on October 20, 2016 6:34 PM

Hey Lucio I was looking at your code here, you stated you got the error

cannot find symbol
  symbol:   variable RandomCharacter

Fro what I understand here is that your code is trying to invoke the method getRandomLowerCaseLetter() on RandomCharacter. So RandomCharacter has to be an object or a variable, I am thinking more like its an object. But no where have you instantiated(declared) that object.

So for the code to work you have to instantiate RandomCharacter


Search our site

Questions

Login above to
see classes

About us

We are a website where students can find answers to unique questions from their classes.