Unable to create new object - java

I created these two files in java and they don't compile. This error comes up:
cannot find symbol C02FootprintV1".
Why doesn't the program recognize the object? I am new to this.
How could I fix this problem?
public class CO2FootprintV1 {
private double myGallonsUsed;
private double myTonsCO2;
private double myPoundsCO2;
CO2FootprintV1(double gals) {
myGallonsUsed = gals;
}
public void calcTonsCO2() {
myTonsCO2 = myGallonsUsed * 0.878;
}
public double getTonsCO2() {
return myTonsCO2;
}
public void convertTonsToPoundsCO2() {
myPoundsCO2 = myTonsCO2 * 220462262;
}
public double getPoundsCO2() {
return myPoundsCO2;
}
}
public class CO2FootprintV1Tester {
public static void main(String[] args) {
double gals;
double tonsCO2, poundsCO2;
gals = 1300;
CO2FootprintV1 object = new C02FootprintV1(gals);
object.calcTonsCO2();
tonsCO2 = object.getTonsCO2();
object.convertTonsToPoundsCO2();
poundsCO2 = object.getPoundsCO2();
}
}

On the line
CO2FootprintV1 object = new C02FootprintV1(gals);
you have C02 (see zero two) on the right hand side, you meant for it to be
CO2FootprintV1 object = new CO2FootprintV1(gals);
or CO2 (see oh two). Also, you should consider that the error messages your tools give you might be correct.

Just change:
CO2FootprintV1 object = new C02FootprintV1(gals);
to:
CO2FootprintV1 object = new CO2FootprintV1(gals);
That's why it is important to have good naming practice.

You put a "0" (cero) instead of an "O" (letter):
CO2FootprintV1 object = new C02FootprintV1(gals);
Try this:
CO2FootprintV1 object = new CO2FootprintV1(gals);

Related

how to combine two generic queues together?

public class QueueDemo<T> {
public static <T> ArrayUnbndQueue<T> mergeQueue(ArrayBndQueue<T> q1, ArrayBndQueue<T> q2) {
ArrayUnbndQueue<T> temp = new ArrayUnbndQueue<T>();
while (!q1.isEmpty()) {
T x = q1.dequeue();
temp.enqueue(x);
}
while (!q2.isEmpty()) {
temp.enqueue(q2.dequeue());
}
return temp;
}
public static void main(String[] args) {
ArrayBndQueue<Integer> q1 = new ArrayBndQueue<Integer>();
ArrayBndQueue<Integer> q2 = new ArrayBndQueue<Integer>();
ArrayUnbndQueue<Integer> q3 = new ArrayUnbndQueue<Integer>();
q1.enqueue(1);
q1.enqueue(2);
q2.enqueue(5);
q2.enqueue(6);
q3.mergeQueue(q1, q2); // i get an errorThe method
//mergeQueue(ArrayBndQueue<Integer>,
//ArrayBndQueue<Integer>) is undefined
//for the type ArrayUnbndQueue<Integer>
}
}
I have a method that adds two queues and returns them together as a new queue, I am not very experienced with generic datatypes which I believe its whats causing the error.
You do not have any method mergeQueue for ArrayUnbndQueue but for QueueDemo.
Therefore you have to do this statement within your main method of QueueDemo:
ArrayUnbndQueue<Integer> q3 = mergeQueue(q1, q2);
The problem does not relate to generics.

Java - Set capable of referencing instances of a different class

Can anyone help me with this java code?
I have two classes and need to create a set that can store instances of the other class. here is the sample I managed to "commit" :)
Class A
public Class Rabbit {
private String age;
//constructor for instance of Rabbit <br>
public Rabbit(String rabAge) {
super();
this.age = rabAge;
}
now class B:
public class ManyRabbits {
private Set <String> setOfRabbits;
now this method should create a new instance of the Rabbit and add it to the set represented by variable setOfRabbits
public void addRabbit (String age)` {
//and I don't know what should go next...something like: `
Rabbit r1 = new Rabbit("10");` <br>
setOfRabbits.add(r1);
}
You need to change your set declatation :
private Set<Rabbit> setOfRabbits;
And you also need to ovweride those two method in order to never insert twice the same object in the set.
#Override
public int hashCode() {
// Your own implementation
return ...;
}
#Override
public boolean equals(Object o) {
// Your own implementation
return ...;
}
your ManyRabbits calss should be like this
public class ManyRabbits
{
private Set<Rabbit> setOfRabbits = new HashSet<Rabbit>();
public void addRabbit (String age)
{
Rabbit r1 = new Rabbit(age);
setOfRabbits.add(r1);
}
}
public class Rabbit {
private String age;
//constructor for instance of Rabbit
public Rabbit(String rabAge) {
super();
this.age = rabAge;
}
// more methods if necessary
}
In ManyRabbits:
public class ManyRabbits {
private Set <Rabbit> setOfRabbits = new HashSet<>();
public void addRabbit (String age) {
Rabbit r1 = new Rabbit(age);
setOfRabbits.add(r1);
}
// more methods if necessary
}
Use it by calling something like
ManyRabbits manyRabbits = new ManyRabbits();
manyRabbits.addRabbit("10");
manyRabbits.addRabbit("20");
manyRabbits.addRabbit("30");
Say:
Set<Rabbit> setOfRabbits = new HashSet<>();
setOfRabbits.add(new Rabbit());
When the Set will be keeping track of an unknown quantity of an object, I typically try to instantiate with an unnamed instance. This would work great for adding to a listener so that you could, say, add a new Rabbit every time the user clicks.
In this way, you could add however many Rabbit object you want. Try:
for(i = 0; i < 10; i++) {
setOfRabbits.add(new Rabbit());
}
You may see this in older versions of Java:
Set<Rabbit> setOfRabbits = new HashSet<Rabbit>();
but it is no longer necessary

ArrayList<HashMap> returning empty in tests and UI?

Here's my code. I apologize for the sloppiness but essentially what it's supposed to do is simulate the backwards learning algorithm used by switches. The handleInput method takes in the src and dest MAC addresses and a port number and adds the src MAC and port# as a HashMaps into an ArrayList. The whole method is useless right now because none of the HashMaps stay in the ArrayList for some reason. Any help is much appreciated!
public class Switching {
ArrayList<HashMap> switchTable = new ArrayList<HashMap>();
public String handleInput(String srcMacAddress, int portNumber, String destMacAddress){
String output = "";
HashMap tableEntry = new HashMap();
tableEntry.put(srcMacAddress, portNumber);
for (HashMap hm : switchTable) {
if (hm.containsKey(destMacAddress)) {
output += hm.get(destMacAddress).toString();
} else {
output += "Ports flooded";
}
}
switchTable.add(tableEntry);
return output;
}
public ArrayList<HashMap> getTable(){
return switchTable;
}
public class SwitchingTests {
#Test
public void testSwitching(){
new Switching().handleInput("123456", 12, "abcdef");
ArrayList<HashMap> switchingTable = new Switching().getTable();
Assert.assertEquals(switchingTable.toString(), "[{123456=12}]");
}
}
You are creating a Switching object and call handleInput(...) on it and then proceed to create a new Switching object and get its table.
You need to get the table from the one you already created.
public class SwitchingTests {
#Test
public void testSwitching(){
Switching switching = new Switching();
switching.handleInput("123456", 12, "abcdef");
ArrayList<HashMap> switchingTable = switching.getTable();
Assert.assertEquals(switchingTable.toString(), "[{123456=12}]");
}
}
inside your handleInput method you are creating new switchTable.
what you have to do is to change your line
switchTable = new ArrayList() to switchTable = getTable();

Converting an array of one type to an array of a subtype

I want to convert an array from one type to another. As shown below, I loop over all objects in the first array and cast them to the 2nd array type.
But is this the best way to do it? Is there a way that doesn't require looping and casting each item?
public MySubtype[] convertType(MyObject[] myObjectArray){
MySubtype[] subtypeArray = new MySubtype[myObjectArray.length];
for(int x=0; x < myObjectArray.length; x++){
subtypeArray[x] = (MySubtype)myObjectArray[x];
}
return subtypeArray;
}
You should be able to use something like this:
Arrays.copyOf(myObjectArray, myObjectArray.length, MySubtype[].class);
However this may just be looping and casting under the hood anyway.
See here.
I would suggest working with List instead of Array if possible.
Here is how to do it:
public class MainTest {
class Employee {
private int id;
public Employee(int id) {
super();
this.id = id;
}
}
class TechEmployee extends Employee{
public TechEmployee(int id) {
super(id);
}
}
public static void main(String[] args) {
MainTest test = new MainTest();
test.runTest();
}
private void runTest(){
TechEmployee[] temps = new TechEmployee[3];
temps[0] = new TechEmployee(0);
temps[1] = new TechEmployee(1);
temps[2] = new TechEmployee(2);
Employee[] emps = Arrays.copyOf(temps, temps.length, Employee[].class);
System.out.println(Arrays.toString(emps));
}
}
Just remember you cannot do it the other way around, i.e. you cannot convert Employee[] to a TechEmployee[].
Something like this is possible if you fancy
public MySubtype[] convertType(MyObject[] myObjectArray){
MySubtype[] subtypeArray = new MySubtype[myObjectArray.length];
List<MyObject> subs = Arrays.asList(myObjectArray);
return subs.toArray(subtypeArray);
}

cannot find symbol symbol: method location: class

Sorry, just learning Java; but, can someone tell me why I'm getting a "cannot find symbol" error?
My code is as follows:
public class NumberHolder {
public int anInt;
public float aFloat;
public NumberHolder(int setAnInt, float setAFloat) {
setAnInt = anInt;
setAFloat = aFloat;
}
public static void main(String[] args) {
NumberHolder newNumber = NumberHolder(12, 24F);
}
}
Looks like you're missing a new before the call to the constructor:
NumberHolder newNumber = new NumberHolder(12, 24F);
EDIT:
Also, as Tassos Bassoukos points out in his answer, you need to turn around the assignments in the constructor:
anInt = setAnInt;
aFloat = setAFloat;
Although personally, I like to write my constructors like this:
public NumberHolder(int anInt, float aFloat) {
this.anInt = anInt;
this.aFloat = aFloat;
}
This is a matter of style and personal preference, though.
Since
public NumberHolder(int anInt, float aFloat);
is a constructor and not an ordenary method, you need to use the keyword new in order to obtain the actual object. You are calling it like a method and you don't have any method named NumberHolder (but it would be valid if you'd have)
Beyond the new keyword that you're missing, the assignment in the constructor should be the other way around.
You need to instanciate new objects with the new keyword.
public class NumberHolder {
public int anInt;
public float aFloat;
public NumberHolder(int anInt, float aFloat) {
this.anInt = anInt;
this.aFloat = aFloat;
}
public static void main(String[] args) {
NumberHolder newNumber = new NumberHolder(12, 24F);
}
}

Categories