How To Write A Constructor Where Can Pass 1 Or 2 Arguments

Apr 6, 2014

I am looking to write a constructor (in a herited class) to initialize the name of a single piece and its orientation using values ​​passed as parameters. If no value is given for guidance , it returns "none" , I do it in this way but i am not sure it is the right way:

public SimplePiece(String piece,String d){
super(piece);
if(d == null){
guidance = "none";
}else{
guidance = d;
}
}

What do you sugger me to do to get the right code .

View Replies


ADVERTISEMENT

Method Is Declared To Take Three Arguments - Only Two Arguments Pass On Program Call

Jun 9, 2014

A method is declared to take three arguments. A program calls this method and passes only two arguments.Will it take the third argument as

1 null
2 void
3 zero
4 Compilation error

View Replies View Related

Two Ways To Pass Arguments To Methods

Nov 14, 2014

I have a test that covers Objects & Classes, Importing Classes and Polymorphism. One of the essay questions will be: Explain two ways to pass arguments to methods and give examples. I was reading the book and found Pass by Value and Pass by Reference. Is this the two ways to pass arguments?

View Replies View Related

Pass Arguments From Main To Paint Method

Feb 5, 2015

I want to take command line arguments and pass them to a paint method. This is a test program that will just draw some equations. How can I get the input array clinputs[] to be used in public void paint( Graphics g) ?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LinePlot extends JFrame {
public LinePlot() {
super( "Line Plot" );
setSize(800,600);

[Code] .....

View Replies View Related

How To Pass Main Arguments On Execution To Swing Component

Aug 4, 2014

I wish to use args passed to main(String[]) during application execution to a Swing component.

In a simple example, assume args passed are: a b c d

And here is the result I look for:

package argsinswing;
import javax.swing.JOptionPane;
public class ArgsInSwing {
public static void main(String[] args) {
String str1 = args[0];

[Code] ....

View Replies View Related

ArrayList In No Arguments Constructor

Sep 29, 2014

I have been able to create a no arg constructor and pass empty double, int, and strings but i am not able to pass an arraylist.

class Savings {
private double balance;
private ArrayList <String> transaction = new ArrayList();
public Savings (double B)/>
{
balance = b;
}
public Savings()
{
this(0.0,new ArrayList());
}

View Replies View Related

Constructor Arguments Error When Compiling

Oct 18, 2014

I am trying to create an array where each object includes a service description, price, and time in minutes. I have a Service class and a SalonReport class:

import java.util.*;
public class Service
{
public String serviceDescription;
public double servicePrice;
public int timeMinutes;

[Code] ....

When I compile this, I get an error message that says:

SalonReport.java:8: error: constructor Service in class Service cannot be applied to given types;
salonServices[0] = new Service("Cut", 8.00, 15);
^
required: no arguments
found: String,double,int
reason: actual and formal argument lists differ in length

What this error message means and how I can correct it? I am confused because my SalonService() method has (String service, double price, int minutes), and each object is listed in that exact order.

View Replies View Related

How To Pass Filename Into Constructor

Feb 26, 2015

I'm taking inheritance now and some things look confusing. I'm trying to do the following but the code, in particular, the constructor in subclasses are pointed to be wrong. The constructor in LongestWord is not correct,how to pass the filename into the constructor?

Java Code:

import java.util.Scanner;
import java.io.File;
public abstract class FileProcessor {
protected Scanner input;
public FileProcessor(String filename) throws Exception

[Code] ....

View Replies View Related

How To Pass Info To Constructor From A Text File

Feb 9, 2015

Suppose there're two classes: Exam and MainExam (contains a main method). class Exam has a constructor public Exam(String firstName, String lastName, int ID). Class MainExam reads data from a textfile. For example, the data can be: John Douglas 57. How can one pass data to the constructor from a textfile?

View Replies View Related

Write Constructor Header - Invent Some Fields?

Oct 18, 2014

I have been given a question about a object creation resulting in the constructor of the Time class being called, and to write the constructor's header new Time(21, 39, 55);

So, this would be simply "public Time" and have to give meaningful names to formal parameters, which is what i don't understand. Is it just private int hours; etc? I have also been told to attempt to write the body of the constructor and will need to estimate how the code in the body will work and to invent some fields.

View Replies View Related

Pass Private Final Class Object To Another Class Constructor

Aug 28, 2014

can we pass private final class object to another class constructor?

View Replies View Related

Overloading Variable Arguments

Dec 27, 2014

I am going through Thinking in Java, 4th Ed and I came across the section that talks about overloading variable arguments. I tried out a piece of code from the book. (Method and class names not exactly the same).

public class Varargs
{
public static void m1(Character... args)
{
System.out.println("Character");
 
[code]....

In the above code, the compiler throws an 'Ambiguous for the type varargs' error. The error goes away if the first method is changed to:

public static void m1(char c, Character... args)

why there is ambiguity in the first piece of code and not the second.

View Replies View Related

Command Line And Two Arguments

Sep 14, 2014

i have to run my program from the command line...My code is:

public static void main( String[] args )
{
 
// check command-line arguments
if ( args.length == 2 )
{
// get command-line arguments
String databaseUserName = args[ 0 ];
String databasePassword = args[ 1 ];
 
[code]....

Now everything works in Netbeans but running it from the command line, i get an error message ".java uses unchecked and unsafe operations".I have added a bit more code to the code above

for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]: "
+ args[i]);
}

Just not sure how to run it from the command line,

View Replies View Related

Using A Button To Run Executable With Arguments

Nov 8, 2014

I am new to JAVA. I need to execute a program, based on the OS. If I am on Mac, the program is a .x and if It is on Windows the program is .exe. The program also requires a line of commands attached to it (i.e. relap5.(x) or (exe) -i inputFile -o outputFile -r restartFile -s stripFile

Here is my coding

String in = " -i ", tfIntdta.getText();
String rst = " -r ", tfRstplt.getText();
String out = " -o ", tfOutdta.getText();
String strp = " -s ", tfStpdta.getText();
if tfStpdta.contains(".csv")
String run = in, rst, out, strp;
else
String run = in. rst, out;
// I want to execute either a .x file or .exe file, depending on if I am
// running the app on windows or mac
run relap5.(x) or (exe) (string run goes here)

View Replies View Related

Alternative To Update Passed Arguments?

Apr 22, 2015

I am writing a simple program in Java, where I call a method and pass some variables (namely, min and max) as arguments. This method is recursive, so the same arguments are again passed on in each recursive call.

I need to update these variables in recursive calls so that when the calling method uses them, it uses updated values. As it might sound confusing, here is sample code :

// Function 1.
void func1() {
//Call func2.
func2 (int hd, int min, int max, Map<String, String> map);
//Other stuff.
}
// Function 2.

[code]....

As you can see, min and max are updated after each recursive call returns, based on conditions. However, these changes aren't reflected in original min and max, which were used by func1 while calling func2. As far as I know, this happens due to call by value mechanism being used by Java while passing arguments. If I declare min and max as instance variables in the class, my problem is solved. But, I want to know whether there's any other way to update these variables so that changes in original min and max are reflected. Yes, I can return them as an array of 2 elements each time while returning, but it didn't seem a good solution to me.

View Replies View Related

Object Arguments In Date Formatters?

Mar 8, 2014

For the object arguments in date formatters such as %td, are only calendar objects accepted?

View Replies View Related

Equals Signature / Constructors And Arguments

Jul 14, 2014

In Java code, write class called Student with the following features:

• a private instance variable int studentNumber that is initialized to zero.
• a private instance variable String firstName;
• a private instance variable String lastName;
• a constructor which takes an integer argument and two String arguments to initializes the three respective data items.
• a public method with signature equals(Student s) . . .

So far this is my code :

public class student {
private int studentnumber = 0;
public student () {
firstname= "forename":
lastname="surname":
public student (integer studentnumber, string firstname, string lastname) {
this.firstname= firstname
this.lastname= lastname:

My question is how do i add the integer in the argument do i have to use int =? and how would i go about doing the public signature equals...

View Replies View Related

Cannot Infer Type Arguments For ArrayList

May 1, 2014

At the beginning of one of my classes, I instantiate some List objects. For example ...

Java Code: private List<> byName = null; mh_sh_highlight_all('java');
... then later, I reference this again in a method like this:

Java Code: byName = new ArrayList<>(rs.getString("firstName"), rs.getString("Last Name")); mh_sh_highlight_all('java');

This is where I get the error. I've tried making the list of <String>, but that didn't make a difference.

View Replies View Related

Using Command Line Arguments As Parameter

Mar 1, 2015

What I'm supposed to do is use the time class and take the command line arguments and print them as the start and end times and then calculate the elapsed time between the two. My issue (hopefully my only as I have been working on this all day now) is that I cannot call the command line arguments using LocalTime. Below is what I have so far.

import java.time.Duration;
import java.time.LocalTime;

public class Clock {
private LocalTime startTime;
private LocalTime stopTime;
//default constructor-initialize startTime to current time
public Clock(){
this.startTime=LocalTime.now();

[Code] ......

View Replies View Related

Passing Primitive Data Type Arguments

Jun 5, 2014

I am totally new to Java. What is the purpose of this method?

Flow of the int x=3; like where does the 3 go step by step?

Passing Primitive Data Type Arguments (from oracle java tutorials)

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:

public class PassPrimitiveByValue {
public static void main(String[] args) {
int x = 3;
// invoke passMethod() with
// x as argument
passMethod(x);

[Code] ....

View Replies View Related

Drawing Polygon On Screen - Collections As Arguments

Aug 29, 2014

I have a method that draws a polygon on the screen:

public void poly(List<Point> points) {
//code
}

For the usage of the method, it makes no difference whether points is an array or a List. In fact, switching doesn't even change any of the code in the method since I use a for-each loop. So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?

View Replies View Related

Passing Reference Data Type Arguments

Oct 21, 2012

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) {
// code to move origin of
// circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);

// code to assign a new
// reference to circle
circle = new Circle(0, 0);
}

Let the method be invoked with these arguments: moveCircle(myCircle, 23, 56)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

View Replies View Related

Static Method That Requires No Arguments And Returns No Value

Sep 30, 2014

I am doing a homework assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding. If someone could dumb down their response and tell me what I did wrong

This is what I supposed to do:

1.) Type the following shell for the class:

public class ParadiseInfo
{
}

2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:

public static void main(String[] args)
{
}

3.) Between the braces of the main()Method, insert a call to the displayInfo() method:

displayInfo();

4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:

public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}

This is what I attempted to do: I know it is wrong I am just clueless on where to put the code and why

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}

--- Update ---

I have also tried this:

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{

[Code]...

The very last attempt I only ended up with one error which is:

G:ParadiseInfo.java:3: error: invalid method declaration; return type required
displayInfo();
^
1 error

Tool completed with exit code 1

Java is very new to me and I have no clue on what I am doing wrong.

View Replies View Related

FileWriter - Method Append Is Not Applicable For Arguments

Jun 7, 2014

I am having a bit of difficulty understanding/Using FileWriter. Now by my understanding in the API FileWriter allows to use write at the end of your file, if you have text in there already, correct?

I have a file with people info in it. And I created a GUI which allows people to input that data.

Java Code:

File file = new File("People.txt");
FileWriter fw = new FileWriter(file);
People p = new People(firstName, lastName, email, sex));
fw.append(p);
fw.close() mh_sh_highlight_all('java');

Now, I keep getting an error on the fw.append(p)

Java Code:

The method append(CharSequence) in the type Writer is not applicable for the arguments(People) mh_sh_highlight_all('java');

View Replies View Related

Type Class Is Not Generic - It Cannot Be Parameterized With Arguments

May 16, 2008

I have set up a project in Eclipse 3.1 and am using java 5.0 compiler.

Here's my folder structure in Eclipse

Java Code:

DFSRemoteClientTestClient.java mh_sh_highlight_all('java');
DFS is the project in Eclipse

And this is how it looks my java class

Java Code:

package RemoteClient;
import java.util.*;
// other imports
public class TestClient {
public static void main(String [] args) throws ServiceInvocationException {
// business logic here ....
}
} mh_sh_highlight_all('java');

So, basically, my java class is just a simple class with a main function.

Now when I build my project, using Project->Clean...

Then I get this as an error at the very first line where i specify the package

This is the error:

Java Code: The type Class is not generic; it cannot be parameterized with arguments <T> mh_sh_highlight_all('java');

What's this error and why am I getting this.

View Replies View Related

How To Get (web Logic) Server Start Up Arguments In Java

Mar 18, 2014

I'm trying to get the server start up arguments in java using System.getProperty("arg")  but it returns null value.

I set the argument value in server console----server----star tup----argument(-Darg=dev).

Other than this any configurations are required. I used weblogic 10.3.5.

View Replies View Related







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