I'm writing a program that involves the use of set/get methods. I will submit a sample code of my issue. The format appears to be alright however the program is not returning any data. This is the same format as my actual code. Why this might be happening?
Java Code:
public static void main(String [] args) {
SecondClass object = new SecondClass();
object.setName("Name");
object.getName();
} mh_sh_highlight_all('java');
Java Code:
public class SecondClass {
private String name;
public SecondClass(){
name = " ";
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
} mh_sh_highlight_all('java');
The code is below. The program runs a series of calculations based on data input by the user. My problem is that for the most important thing I'm looking for, total kg CO2 emissions, I continually get an answer of 0.0. What I need is a sum of the individual total emissions as calculated in each method, i.e. the values which are printed with the following: System.out.println(trans); System.out.println(elec); and System.out.println(food);
The total should be something like 25040 or whatever, depending on the value of the inputs provided by the user, but I'm constantly getting a total of 0.0., which is obviously false. Could have something to do with the way I've initialized my variables, or something to do with the limitations of returning values from methods.
import java.util.Scanner; public class CarbonCalc { public static void main(String[] args) { double trans = 0; double elec = 0; double food = 0; giveIntro(); determineTransportationEmission(null);
Regarding return statements within methods. So I have a method containing try and catch block (as required) and much like when you have an if else statement... I noted you have to return an object for both the try and catch blocks. Now in my case my method should return a List object.
The way I have tried to overcome this:
- I've initialised a List object to null as an attribute of the class I'm working in. - Therefore in the catch block would just simply return the null List object, where as the try block would return the non-empty List (which is what I want). - I then just test to see if the List != null, when the method is invoked... and that is that.
However the method always seems to return null (when it shouldn't).
I have a very standard Lab assignment. It's probably been seen a lot. I wrote the first part not realizing I had to write a second class to do use the methods. I'm not sure how to change my program to call methods from my second class instead of doing all my calculations with user input in my first class.
Here's the first class' code:
package tickets; //Imports classes used for "Ticket" application. import java.util.*; import java.text.DecimalFormat; public class Tickets {
[Code] ......
The code is obviously incomplete. I have not tried to compile, nor would I expect it to compile right. I'm not sure how to move my calculations from the first class shown above into my second class and use them as methods.
I am stuck on this part of my assignment. When the answer prints out at the end, the calculation of average is incorrect. This is what the assignment wants:
Write two overloaded methods that return the average of an array with the following headers:
public static int average(int[] array) public static double average(double[] array)
Write a test program that prompts the user to enter ten double values, invokes this method and displays the average value.
public class Week7Arrays2 { public static int average(int[] array) { int sum = 0; int average = 0; for (int i = 0; i < array.length; i++) { sum = sum + array[i];
The question pretty much says it all, but I tasked myself with creating a program about lemurs. There are multiple class files in this program. In the below code snippet, I have my TreeLemur.class which extends to the Lemur.class which extends to the Mammal.class. However, when I create a Tree Lemur object in the main program, it is returning null consistently from certain methods. What am I doing wrong here?
TreeLemur.class :
public class TreeLemur extends Lemur { private String groupSize; private String diet; private String fur; public void setGroupSize() { groupSize = " Group Size: Large"; }
[Code]...
As of yet, I'm just trying to get Tree Lemur working properly to continue with creating the other if-branches within the main program.
I'm having an issue returning data from a constructor. This is an assignment, and the specifications were that two classes are to be used, one for the variables and assigning methods, and one for the main method and the printing. This app compiles, but returns "0" for the isbn number, and I'm sure it's because I'm not doing something right with my constructors. My code is below
public class Book { /* Declare Variables */ public static int isbn; /* Constructor */ public Book (int isbn) { isbn = 454545;
[code]....
There are other variables to add, but if I can get one working, I can get the rest working.
How do I pass the data within an initialized array from inside one method to another method of the same class? Will I need to return the array, assigning it to a temp array, which will then be passed as an argument to the other array? The idea is to create an array for an entire year, and be able to manipulate or edit data for a particular month using the other method.
public class Temperature { static Scanner input = new Scanner(System.in); static String [] monthArray = {"January", "February", "March", "April", "May", "June", "July", "August", "October", "November", "December"}; public static void main(String[] args) {
I have been asked to write a program with 3 classes(an interface, a store and a product class) and I have been going ok until I need to display data on the interface that is held in the product class. At the moment the code will compile fine but when I run the program and try to use the writeOutput() method I get a stack overflow error.
I realise now that it is because of a non-terminating recursive call, however I can not think of a solution on how to fix the problem. And just to clarify, what I am aiming to do is have the MatesTerminal class display the data for name that is stored in the Product class(I have no way of determining which product to display at this time, so I would like to be able to display the data for all three products if possible). Anyway this is what I have so far:
The method from the MatesTerminal Class:
private void writeOutput() { int productChoice; Scanner console = new Scanner(System.in);
I have to make two classes. The first one crates an instance of an array of several integers and prints data (average, greatest, lowest, et cetera) based on the second class, which contains the methods. I'm having a problem with the syntax for the first class required to use the methods.
Here's a shortened version of what I have right now just based on processing the number of integers in the array (because if I can get just one method properly connected, I could figure out everything else).
Driver
import java.util.Arrays; public class ArrayMethodsDriver { //Creates the ArrayMethods object public static void main(String[] args) { int[] a = {7,8,8,3,4,9,8,7};
[Code] ....
When I try to compile this, I currently get the "class expected" error on the count part.
I'm new to Java and I have a problem with a method, I can't see the code of the method, I just have a jar, but it should return a boolean, something like this:
boolean band = false; band = TestClass.testMethod("blabla"); // band = false
The problem is that the method seems that is returning nothing (band remain false), and if I initialize band to true:
boolean band = false; band = TestClass.testMethod("blabla"); // band = true
band remain true, in other words, the value of band is never modified, the question is, how is this possible? because it should return the same value on both calls, true or false, no matter the initial value of the variable that is receiving the returning value of the method.
This simple program should build a new table with random values and on the getValue method (perhaps called from othe TestClass) return back content of particular cell. why my getValue method doesn't work. Nay! It returns error while compilation.
import java.util.Random; class TablicaIntowa{ public int getValue(int a){ return tabl[a]; } //getValue method end
I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).I need to return the values of the array,Here is my class:
public class example{ double[][] Position=new double[2][11]; double calculate(){ for (int time=0;time<=10;time=time+1){ Position[1][time]=time; Position[2][time]=time+1; double A=Position[2][time]; return A; } } }
I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A).
I've made a class called Car with a method which will tell me a category for the engine size regarding the actual size (which I've included in the main just so I could see if it works) but everytime I test it I get an error.
public class Car { public String b; public String c; public double es; public double cs; public String getCategory() { if (es < 1.3)
[Code] ....
Figured it out. Was missing parenthesis on audiCar.getCategory();
i am trying to run a command in terminal the code is below if i run the command in terminal it works fine however when i run it from netbeans with code below nothing gets printed. however if i run a different command such as (ip addr) it works fine?
public static void a() throws IOException{ ArrayList lister=new ArrayList(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("ps -ef | grep firefox");// the command i am trying to run to get pid of application InputStream stderr = proc.getInputStream();
I am writing a code where in the first method the question will ask whats your favorite website. for example www.javaprogrammingforums.com...when it outputs it will read just "javaprogrammingforums" without the www. and the .com.
Because the program will ask a series of questions in the main, I would like website question to be returned to the main. Here is my code, and what can I do?
import java.util.Scanner; public class chapter3 { public static String website(Scanner kb) { String website; System.out.println("What is your favorite website?"); website = kb.next();
For some reason my code returns the memory address of the array when its a print statement with a string, but it works fine when its in a separate print statement all by itself. Why? Do I need to create a toString method that converts a char array to a String to something? The reason why I ask that is becuase on Eclipse line 10 has a warning stating "Must explicitly convert char[] to a String".
public class Ex { private String word; public Ex(String word) { this.word = word; } public char[] Display(){ char[] wordChars = this.word.toCharArray(); return wordChars;
[Code] .....
Result:
Hello world The word is: [C@1db9742
I also tried this, knowing that it's a long shot, but that didnt do anything...
public String toString(){ Ex ex = new Ex(this.word); char[] word = ex.Display(); String updated = word.toString();//counter intuitive? return updated; }
I have a for each loop that outputs the Mechanics salary for the first team in the arraylist, how can I add these figures together instead of having separate value for each?The code in my testing class to get the salary for mechanics:
Java Code: for (Mechanic str1: formula1.get(0)){ System.out.println(str1.getSalary()); } mh_sh_highlight_all('java');
How can I also get the salary for the driver 1 + 2 in the same team and add their salary to this? I have attaached an image showing the classes/fields created
public int[] allIndicesOf(E itemSought) { ArrayList<Integer> toUse = new ArrayList<>(); for (E anArray : container) { if (anArray.equals(itemSought)) { toUse.add(container.indexOf(itemSought));
[Code] ....
I have an array list of strings. I want to be able to return an array of integers telling me which indexes in the string array list contain the itemSought object.
I am working on a project which manages an airport's airplanes and flights based on user input. The method printFlights() - lines 133-134 - is returning null and I can't figure out why. The method is supposed to print information about each flight. The logic is identical to the printPlanes() method which is working successfully.
When I try to getNextMailItem,it keeps returning null.Why?
public class MailServer { // Storage for the arbitrary number of mail items to be stored // on the server. private HashMap<String,ArrayList<MailItem>> mailbox; /** * Construct a mail server. */ public MailServer()