How To Use Mutator Method

Jul 31, 2014

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....

View Replies


ADVERTISEMENT

Using Mutator Method To Set Data To Array Element

Jun 2, 2014

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" .....

View Replies View Related

Creating Accessor And Mutator For Dual Array?

Apr 19, 2015

I am familiar with creating accessors and mutators, Any example of how to create them for dual arrays? I need the dual arrays to be int

View Replies View Related

RetailItem Class Modify With Accessor Mutator

Apr 23, 2014

Here is what i have so far. The retaiItem class and the demo. Basically, my output looks like this:

Description:
Candy bar
Item Number: 17789
Wholesale Cost: $0.75
Retail Price: $1.50
new wholesale cost: 1.0
new retail price: 1.99

[Code] ....

package retailitemdemo;
public class RetailItemDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

[Code] .....

View Replies View Related

Sphere Class Using Accessor / Mutator Methods

Feb 25, 2014

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

[Code] ....

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

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?

View Replies View Related

Writing A Method That Returns Words Without Four Letters Back To Main Method

May 27, 2014

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

View Replies View Related

Recursive Method - Calculate Greatest Common Divisor Using Euclidean Method

Apr 29, 2014

Consider the following recursive method that calculates the greatest common divisor using Euclidean method.

PHP Code:

public static int GCD ( int x , int y )
{
    if ( y == 0 )                        
        return x;
    else if ( x >= y && y > 0)
        return GCD ( y , x % y );
    else return GCD ( y , x );  


Trace the above method for x=32 and y=46

View Replies View Related

How To Link Compress Method To Return Statement Method GetPText

Oct 30, 2014

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.

import java.util.Scanner;
class RunLengthCode {
String pText;
String cText; 
void setPText(String PText) {
pText = "";
}

[Code]...

View Replies View Related

Altering Parent Method Behavior By Overriding Method It Calls

Apr 21, 2014

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.

View Replies View Related

How To Call A Method That Exist Within A Class Into Main Method

Feb 13, 2014

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();

[code]....

View Replies View Related

Which Method Is Used While Passing Or Returning A Object From The Native Method

Mar 5, 2014

Which method is used while passing or returning a java object from the native method?

View Replies View Related

Calling Private Method To Another Method Located In Same Class

Oct 23, 2014

I am trying to call a private method to another method that are located in the same class.

I am trying to call prepareString to the encode method below.

Java Code:

public class ShiftEncoderDecoder
private String prepareString(String plainText)
{
String preparedString = "";
for(int i = 0 ; i < plainText.length();i++)
if(Character.isAlphabetic(plainText.charAt(i)))

[Code] .....

View Replies View Related

Dice Game - Invoking Method In Other Method Of Same Class

Feb 26, 2015

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?

View Replies View Related

Set Method Return Type Determined Inside Method

Dec 25, 2014

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..}

View Replies View Related

Invoking Non-Existent Method - Matching Parameters To Method

Oct 17, 2014

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);

[Code] ....

View Replies View Related

Possible To Check If Classes Contains A Specific Method And Then Run That Method?

Oct 5, 2014

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!

View Replies View Related

OOP A Method Being A Return Type In A Method?

May 21, 2015

I stumbled upon a method that im not sure how to handle (yet).Below the code:

public DateTime() {
this.day = day(milliseconds);
this.month = month(milliseconds);
this.year = year(milliseconds);
this.hour = hour(milliseconds);
this.minute = minute(milliseconds);

[code]...

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?

View Replies View Related

Method Overridden In Method Parameter?

Jul 20, 2014

I was following a tutorial for libGdx and suddenly became confused by the following syntax:

up.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y){
position.y += 1f;
//currentFrame = animation.getKeyFrame(12);
}
});

"up" is basically a text Button:

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?

View Replies View Related

Method Call For A Void Method

Sep 29, 2014

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?

View Replies View Related

Main Method Separated From Other Method

Jan 13, 2014

Here's a class called Fibonacci

Java Code:

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?

View Replies View Related

Use Parameter Of Method One In Second Method

Nov 27, 2014

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.

View Replies View Related

Why Method Is Not Calling Another Method

Feb 10, 2015

Why is the method readAllExams(Scanner s) giving an error on calling another method which is readExam(Scanner s )?

Java Code:

public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException

{
String firstName = "";
String lastName = "";
int ID = 0;
String examType = "";
int score = 0;
int index = 0;
Exam[] object = new Exam[50];

[Code] ....

View Replies View Related

Static Method Accessing Non Static Method

Oct 6, 2014

class Test
{
static Test ref;
String[] arguments;
public void func(String[] args)
{
ref.arguments = args;

[code]...

This code run perfectly.But the static method is acessing a non static method which was supposed to be not possible.

View Replies View Related

Calling A Non Static Method From Static Method

Jun 24, 2014

We know that a non static method can not be directly refrenced from static method but in the example given below there is no such error.Why?

class A
{
private void display()
{
System.out.println("In A");
}

public static void invoke (A x)
{
x.display();

[code]....

In above case a non static method display() is being invoked from static method invoke ()but it is not giving error.why?

View Replies View Related

DOM Parsing Method

Feb 26, 2015

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:

<?xml version='1.0' encoding='UTF-8' ?>
<form>
<record event="boot" date="2013-11-01">
<text>system boot</text>
</record>
<record event="login" host="localhost" date="2013-11-01">

[code].....

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).

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved