So I am doing exercises from my book and I am stuck for nth time. I have problems understanding the exercises from this chapter, this one as well... Add the following method to the Stock class:
public void clear()
Resets this Stock’s number of shares purchased and total cost to 0.
So I did this : public void clear(){
this.totalShares = 0;
this.totalCost = 0.0;
My problem is I don't know where and how to use it in my code and what result should I look for.Here is a pastebin with the Stock class URL....
the problem I'm having is I want to use a mutator method to set data for an array element. The code I have so far is:
public void addProduct(String productName) //Goes through and sets the name of a product and assigns it to the array { int index; for (index = 0; index < product.length(); index++) { product[index].setName(productName); numberOfProducts++; } }
The array was initialised like this:
Product[] product = new Product[3];
And the setName(String) method is just your typical mutator method.However, in Eclipse, I have an error messages. It is:
"-The method setName(String[]) is undefined for the type String" .....
So I've been working on this sphere class forever and have tried rewriting it a number of times. each time i change something i get a different error...
Instructions: create a sphere class with the following properties:
private attributes: x, y, z coordinates of center, and the radius.
Accessor and mutator methods to: set and get the x,y,z coordinates, set and get the radius, get the volume and surface area of the sphere.
This is the main sphere class:
Java Code:
package spheretester; <pre class="brush:java;"> */ import java.io.*; import java.util.Scanner; public class Sphere { public static void main(String[] argv) throws Exception
I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?
I have to write a method called censor that gets an array of strings from the user as an argument and then returns an array of strings that contains all of the original strings in the same order except those with length of 4. For example if cat, dog, moose, lazy, with was entered, then it would return the exact same thing except for the words with and lazy.
Currently, my code so far just prints [Ljava.lang.String;@38cfdf and im stuck.
import java.util.Scanner; public class censorProgram { public static void main (String args[]){ Scanner input = new Scanner (System.in); System.out.println ("How many words would you like to enter?"); int lineOfText = input.nextInt();
[Code] ....
I have to make new string array in the method and return words without four letters in the main method
Alright, I don't understand how to link my compress method to my return statement method "getPText". Also in my compression I only want it to compress for 3 or more consecutive letters.
I have two classes (Daughter and Son) that contain some very similar method definitions:
public class Family { public static void main(String[] args) { Daughter d = new Daughter(); Son s = new Son(); d.speak(); s.speak();
[Code] .....
Each of those classes has a "speak" method with two out of three lines being identical. I could move those into a parent class, but I need each of the child classes to continue to exhibit its unique behavior. I'm trying the approach below, which replaces the unique code with a call to a "placeholder" method that must be implemented by each child class:
public class Family { public static void main(String[] args) { Daughter d = new Daughter(); Son s = new Son();
[Code] .....
This works and moves the shared code from two places (the Daughter and Son classes) into one place (the new Mother class, which is now a parent class of Daughter and Son). Something about this feels a bit odd to me, though. It's one thing for a child class to override a parent class's methods to extend or alter their behavior. But, here, I've implemented an abstract method in the parent class to alter what happens when the parent class's method (speak(), in this case) is called, without overriding that parent class method itself.
I am just trying to test this array, for a locker combination program that involves classes...but the array is printing out the whacky numbers for the location. When I try to call the method in the main, it does not work. How do I call a method that exist within a class into the main method?
public class locker { public static void main(String[] args) { CombinationLock();
I am currently working on a dice game. I have a private method called rollDice and it performs the action of rolling two dice. For my project, I need to create another method called playerRolls and I am supposed to invoke the rollDice method in the playerRolls method and perform actions based off of that. My question right now is how do I invoke a method into another method of the same class?
I need to write a method that will consume string representation of Object type and will return one object of this type. How to set return type for the method in this case?
Here is exmaple :
public <?> identifyType(String typeString){ if (typesString.matches("String")){ return new String(""); }else if (typeString.matches("Integer")){ return new Integer(0); } //....etc..}
Java-code, When i compile the java doc. I get output;
Perceptron.java:12: learn(Instance[],int,int) in Perceptron cannot be applied to (Instance[],int) PerceptronModel model = learn(train_data,5); ^ 1 error
And here is the code
import java.io.*; public class Perceptron { public static void main(String[] args) throws IOException { DataReader reader = new DataReader(); reader.init(args);
I'm working on a simple 2D Game Framework. Currently it's creating a window, but I'm working on an update method.
I constantly export the framework as a .jar file, and I've added it to the game's build path (using eclipse). I wonder if I can check all classes in the game-project (the project in which I've refecrenced the Framework in) for a specific method (for example update()) and call all found methods with that name from the framework's main class, like this:
public class FrameworkClass1 { private void checkForUpdateMethods() { // Check for update methods on program start } public void update() { runAllFoundUpdateMethods(); // Run all update methods found in the scan } }
I want to do this because it would be a simple way to update and render the game. If the main game-class look something like this:
public class Game { public void update() { // Update the game every time this is ran } }
it will be automatically updated, because it contains a method named update(), instead of naming the main game-class with a specific name etc. It will simply be more flexible that way!
The two last methods stumped me. The return type to each is "DateTime", according to JUnit complaints.I know that I can use the "this" keyword to reference to the object. But how do I get these two methods to return the correct result?
TextButton up = new TextButton("up", textButtonStyle);
and .addListener is just one of the methods "TextButton" has (actually I think its inherited from "Button" but that doesn't matter).
Basically my question is what's going on inside the parentheses? From what I see its a new instance of "ClickListener" but then suddenly they override an actual method within. Is this simply just a way to override a method from the ClickListener class or is it something else?
I've never had to do a void method call. I have my void method in one class and my main (where I want to do the call) in another. How do you actually call a void method?
public class Fibonacci { public static int fib(int n) { if (n < 2) {return n;} else {return fib(n-1)+fib(n-2);}
[code]....
it contains one method called fib() and one main method.If I would want to have the main method in another class than fib(), how would I write the two classes? Only cutting the main method from this class to another one doesn't work.My question is, is the reason it doesn't work because I then would have to have a constructor in the Fibonacci class, and create a Fibonacci object first which I then use the method on?
I have two methods with parameters out of the main method, both of them work fine alone but I am finding a problem to use them together. I need to use the parameter of Method one in the second Method.
I've been staring at my code for the past few days and I just can't get it to work out. I'm trying to parse a DOM object with the following XML loaded:
I'm trying to get all of the attributes that I care about out of the <record> tags (date, event, etc ...) and stick the data in a wrapper class (DocumentData). If you run this, you'll see that it is close to working, but I'm having trouble getting the attributes in the <subject> and <return> tags (I need to get the errval and uid attributes).