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
ADVERTISEMENT
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
View Related
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
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
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
Mar 22, 2014
import java.util.ArrayList;
public class LectureRoom{
private String courseName;
private String roomNumber;
private String Lecturer;
private ArrayList <Student> studentList;
[Code] .....
Question:
Given the following BlueJ class diagram
Lecturer class (same with previous lab, no changes needed)
Student class (same with previous lab, no changes needed)
LectureRoom (changes occurs here)
1. LectureRoom has roomNumber (e.g. A301), courseName (e.g. Java), lecturer (a reference to a Lecturer object), and studentList (a reference to an ArrayList that stores Student object).
2. LectureRoom has a constructor that receives courseName, roomNumber, and Lecturer. The constructor then sets/assign the courseName, roomNumber and Lecturer. This constructor also creates the studentList arraylist object.
View Replies
View Related
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
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
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
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
Mar 8, 2014
For the object arguments in date formatters such as %td, are only calendar objects accepted?
View Replies
View Related
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
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
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
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
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
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
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
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
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
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
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
Apr 4, 2015
How would I randomly assign colors and a with a name? Some functionality may require command-line arguments but I am a bit confused on how to structure this. Also, these names must appear on a color coded on a gui
View Replies
View Related
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
Feb 19, 2011
I thought static methods could never use instance variables, because they wouldn't know which instance to look at.
From Head First Java, p. 284: "A static method is not associated with a particular instance - only the class - so it cannot access any instance variable values of its class. It wouldn't know which instance's values to use."
Now I was answering some mock exam questions from Cameron McKenzie's SCJA book, and I don't understand one of the options. On page 205, the last question has an option that says: "Instance variables ... are not visible in static methods, unless passed in as arguments." This option is supposed to be correct. Now... how does that work?
View Replies
View Related
Oct 7, 2014
This is titled "Creating static methods that accepts arguments and returns a value". I think that I understood everything about this except for the very bottom part of the code. I wasn't really sure where to put it. From the errors that I am seeing, I know which line is giving the errors but I'm not sure what is wrong with it.
import java.util.Scanner;
public class ParadiseInfo2{
public static void main(String[] args){
double price;
double discount;
[Code] ....
Errors:G:ParadiseInfo2.java:29: error: illegal start of expression
public static double computeDiscountInfo(double pr, double dscnt)
^
G:ParadiseInfo2.java:29: error: illegal start of expression
public static double computeDiscountInfo(double pr, double dscnt)
[Code] ....
6 errors
Tool completed with exit code 1
View Replies
View Related