Passing Values And Objects
Oct 28, 2014
Whether I pass primitives or objects the original value does not change. Is this expected result?
Java Code:
public class Class {
public static void main(String args[]) {
int x=3;
doItPrim(x);
System.out.println(x);//3
Integer i=new Integer(3);
[Code] ......
View Replies
ADVERTISEMENT
Mar 20, 2014
We are learning about how to pass objects into constructors. In our class, we made this following code:
public class InventoryItem {
//fields
private String description;
private int units;
//Add New constructor
public InventoryItem(InventoryItem some_object) {
description=some_object.description;
[Code] .....
As you can see the object of the same class is passed as the argument. Inside the constructor, our teacher did
some_object.description
This constructor, from my understanding, copies the description field and unit field to an object to the new object of class. Meanwhile, here is the demo class.
public static void main(String[] args)
{
//Create object
InventoryItem item1;
item1=new InventoryItem("hammer",20);
System.out.println("Item 1: ");
[Code] .....
Over here, my teacher uses :
.getDescription
My problem is that in the constructor of the first class I showed, why didn't he use
.getDescription
method instead of this
.description
May I know what is difference between these two. If I use .getdescription that would return the value of a field from that object too.
View Replies
View Related
Apr 30, 2015
I'm trying to pass an object to a server and then back again. The object gets to the server fine, but either the server doesn't return it correctly or the client isn't accepting it. There's not error's. The client just prints the object as null. Not really sure why, I looked at other examples and they seem to be the same.
Client:
public class main {
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is" +
"running the service on port 9090:");
Voter you = prompt();
[Code] .....
View Replies
View Related
Mar 1, 2015
I am trying to pass the values for UPPER_BOUND and LOWER_BOUND from the main method into the getValidNumber method. However, I'm not sure how to do this. The rest of the code is correct as far as I know, I just need to get the values for the bounds into the getValidNumber method. How would I do this? the notes in the main method explain what I need to do.
public static void main(String[] args) throws FileNotFoundException {
Scanner kb = new Scanner (System.in);
final double LOWER_BOUND = 0;
final double UPPER_BOUND = 100;
//Call the getValidNumber method, passing it the values LOWER_BOUND and UPPER_BOUND.
//Take the returned value and store it in a variable called num.
//Print out the value of num (here in the main method)
[Code] .....
View Replies
View Related
Feb 19, 2015
I am trying to get the username and password for one class and send them to another. However I cannot send them in the constructor because a database class is getting them and I need that constructor blank. However if I do not pass the class in the constructor I get a NULL error, even better when I do pass it in I seem to get a thread lock. I do try to do my own housekeeping and close the one frame before I open a new one but it doesn't work. I will do my best to show the design of the program.
program starts
Java Code: public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// connects to database and generates a GUI
new UserPromptScreen();
[code]....
I have to pass in UserPromptScreen in for it to work. This will cause me issues later with my design.
View Replies
View Related
Apr 30, 2014
I'm supposed to write a program that reads in 20 numbers stores them into a one dimensional array and then create a method that will calculate the average of the numbers in a separate method.
I've written a for loop in the main method that will take in the numbers but now I need to know how I can pass those values to a method that will calculate the average.
public class ConstructorHomework {
private static double average; //Declaring the Global Variable
//This Method Calculates The Average
public void avg(int x){
average = x/20;
[Code] .....
This is the main method an it contains the for loop that will take in 20 numbers
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int size = 20;
double[] array = new double[size];
for (int i = 0; i < array.length; i++){
array[i] = input.nextDouble();
}
//
Here I was trying to create and object and use it to pass value to the method
ConstructorHomework a = new ConstructorHomework();
a.avg(); //Problem is that I don't know what to put in the brackets
a.showNum();
}
}
View Replies
View Related
Oct 15, 2014
How to pass the value of a field in a database from one view to another. I currently have a list of members drawn from a database.
The members view is a list of members. From the list the user clicks view button to view a particular member. This is achieved by using a inputHidden tag with the value of the list of members in a data table.
<h:dataTable value="#{controllerBean.listAllMembers()}" var="mem">
<h:form id="inputForm">
<h:inputHidden id="idToken" value="#{mem.id}" />
in the RequestScoped bean I am able to use FacesContext to extract the value of idToken from the requestParameters and create the view for the particular member.
One field in the member view I would like to toggle - "Y" to "N" or vice versa via a commandButton. The issue is I can find no way of passing this single value back to the bean as is.
<td>Active:</td>
<td><h:outputText value="#{controllerBean.member.active}" /></td>
<td>
<h:outputLabel value="To change toggle active button below" />
<h:inputHidden id="actToken" value="#{controllerBean.active}"/>
</td>
//further down the view
<h:commandButton style="float: right" value="Toggle Active" action="#{controllerBean.toggleActive()}"/>
The issue is the value is displayed within the output text - how to pass the value into the inputHidden value.
View Replies
View Related
Mar 8, 2015
I have an object that has an instance of another object class as its parameter :
CombinationLock oneHundred = new CombinationLock(28,17,39);
Locker Mickey = new Locker(100, "Mickey", 3, oneHundred);
This is for a locker, which has a combination assigned to the student. Within the locker class I have the following constructor:
public Locker(int locker, String student, int numberOfBooks, CombinationLock combo) {
this.locker = locker;
this.combo = combo;
this.student = student;
this.numberOfBooks = numberOfBooks;
}
combo is the private CombinationLocker object I created within the Locker class. Do I need to pass the combo object on to the CombinationLock class? For reason, I do not comprehend, the combination password from the main class is not passing through to the CombinationLock class, and the combination values are all zero.
View Replies
View Related
Jan 29, 2014
Long story short: The program takes user values (temperature) and converts them to the opposite (C >> F / F >> C)
I originally started this program with three separate arrays but then decided that it would be a good opportunity to use a two-dimensional array and one other.
The two-dimensional array has 2 rows, 10 columns. The second is a normal String array ...
Java Code:
String[][] myTemperatures = new String[2][9];
String inputAssembly[] = new String[9]; mh_sh_highlight_all('java');
I prompt the user for to enter temperature values, using a GUI and jbutton to distinguish F/C. Each time the user clicks 'continue', the values are stored into the two-dimensional array. One row holds the temperature, the other holds the C or F designation.
Java Code:
// CONTINUE BUTTON CLICK ACTIONS
class ContinueButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
input = view.getTempValue();
[Code] ....
This is where I am experiencing the trouble and I cannot seem to get the Debug to work properly here. When the two-dimensional array is full OR the user clicks 'calculate' instead of 'continue', the Calculate event is performed via an ActionListener.
Java Code:
// CALCULATE BUTTON CLICK ACTIONS
class CalculateButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String hold;
Double temp;
And I get a ton of errrors ...
Java Code:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "[Ljava.lang.String;@7441b1fd"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
[Code] ....
I imagine the issue lies within how I am handling the two-dimensional array in the CALCULATE event and/or converting the String[][] to String then parsing to an Integer.
Would this be better done is separate arrays (not using one two-dimensional, but storing 34C, 45F ... in one. I think this would be difficult for me to parse for conversions).
View Replies
View Related
Jan 24, 2014
I just kinda get stuck when it comes to passing values into constructors, using main method or static method functionality. In theory i kind of understand how it work but when i type it, it's totally different! I have to have a junit test too but i guess i could do that in the end.
I have attached the assignment. So, how to proceed with this:
public class Flight {
int flight_number, capacity, number_of_seats_left;
String origin, destination;
String departure_time;
double original_price;
[Code] ....
View Replies
View Related
Feb 14, 2015
I'm doing an aggregation exercise that's suppose to find the volume and surface area of a cylinder. What I'm trying to do is pass values from one class, to a second class, and that second class passes values to a third class.
This may be a clearer explanation: The first class is the main program which sends values to the second and third class. The second class is used do calculations for a circle (a pre-existing class from another assignment). The third class grabs the values that the second class calculated and calculates those values with the one that was passed from the first class to the third class. The first class then prints the outcome.
Problem is when the program gets to the third class, it just calculates the value from the first class with the default constructor from the second class. It's like the second class never received the values from the first class. I think I'm missing a step, but I don't what it is.
First Class:
package circle;
import java.util.Scanner;
public class CylinderInput
{
static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
//user defined variable
[Code]...
View Replies
View Related
Nov 3, 2014
Everything is written up and looks good and i get no compile errors but every student object other than the default constructor has null and zero values when printed.
import java.text.DecimalFormat;
import java.lang.Math;
public class Student
{
//instantiate variables
[Code] .....
View Replies
View Related
Apr 3, 2014
The main method should creates a Student object with name as "Bill" and marks as {88,92,76,81,83} and print it.
Java Code:
class TestStudent {
public static void main(String args[]) {
Student1 st = new Student1();
st.name = "bill";
Student st1[] = new Student[6];
//This gives error
st1[].marks = {1, 5, 8, 9, 7, 6}; mh_sh_highlight_all('java');
View Replies
View Related
May 4, 2014
I've written a die program, and i want the user to type any key in order to continue, how do i achieve this ?
I have already created a Die class, i just want to implement the main method now.
PHP Code:
package test;
/* This program demonstrates the use of a user-defined class
public class RollingDice {
// Creates two Die objects and rolls them display the values.
public static void main (String[] args) {
Die die1 = new Die();
System.out.println("Roll the die ")
}
} mh_sh_highlight_all('php');
What should i add after System.out.println, So that the user types (any key, or types "Enter" for example, for the die to roll). i dont want the die to roll by itself i Want the user to interact with the program in order for it to roll.
View Replies
View Related
May 6, 2014
Create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either ID number or sales value.
import java.util.*;
public class SalesPersonSort
{
public static void main(String args[]) {
int[] id = new int[7];
int[] salesValue = new int[7];
final int END = 999;
[Code] ....
View Replies
View Related
Dec 10, 2014
This program is basically complete. It compiles and runs. It is a college course assignment that I pretty much completed but for the last part in which I'm suppose to change the values of all fields and display the modified values using a toString method. Modifying the values of the fields is where I am stuck. I don't think I need to create a new text data file to do this. The instructor only asked that all the values of fields be changed and this was the last part of the assignment so I don't think it involves creating additional ObjectOutputStream and ObjectInputStream objects. I'm getting a NullPointerException error on line 161.Here is the code. I'm also including the input data file.
//create program so that objects of class can be serialized, implements interface Serialiable
//create constructor with 4 parameters with accompanying get and set methods, Override toString method
//create text file with 5 records, create Scanner object,ObjectOutputStream, and ObjectInputStream
//create new ItemRecord object, change values of all fields in ItemRecord object using object's set methods
//modify ItemRecord object using toString method
[hightlight =Java]import java.io.Serializable;
public class ItemRecord implements Serializable
[Code] .....
This is the error message:
----jGRASP exec: java ItemRecordReport
Item Cost Quantity Description
Number
A100 $ 99.99 10 Canon PowerShot-135
A200 $149.99 50 Panasonic-Lumix T55
A300 $349.99 20 Nikon- D3200 DSRL
A400 $280.99 30 Sony- DSC-W800
A500 $ 97.99 20 Samsung- WB35F
Exception in thread "main" java.lang.NullPointerException
at ItemRecordReport.main(ItemRecordReport.java:161)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Here is the data file:
A100 99.99 10 Canon PowerShot-135
A200 149.99 50 Panasonic-Lumix T55
A300 349.99 20 Nikon- D3200 DSRL
A400 280.99 30 Sony- DSC-W800
A500 97.99 20 Samsung- WB35F
Here is the data file for the modified field values.
B100 98.00 10 ABC1010
B200 97.00 15 DEF1020
B300 96.00 10 GHI1030
B400 95.00 05 JKL1040
B500 94.00 01 MNO1050
View Replies
View Related
Mar 7, 2014
My intention is to see the real reference values (memory locations) of 2 StringBuilder objects as below:
StringBuilder sb2 = new StringBuilder("123");
StringBuilder sb3 = sb2;
System.out.println(sb2);
System.out.println(sb3);
I was expecting a printout like
@S234853
@S984724
But all I got was 123 printed twice. How can i print out the reference values instead of what is inside the StringBuilder object ?
View Replies
View Related
Jul 27, 2014
I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...
public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){
SerializableObject serializableObject = new SerializableObject();
serializableObject.setField(dataObject.getField());
serializableObject.setAnotherField(dataObject.getAnotherField());
return serializableObject;
}
Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.
View Replies
View Related
Aug 29, 2014
I need a way to store the pixels values currently on the screen and compare them to the values on the first frame. Right now I'm using glreadpixels as follows:
currentBuffer= BufferTools.reserveByteData(mapSize);
glReadPixels(mapStartX, mapStartY, mapWidth, mapHeight, GL_BLUE, GL_UNSIGNED_BYTE, currentBuffer);
for (int i = 0; i < mapSize; i++) {
if (currentBuffer.get(i) != baseBuffer.get(i)) {
//Do nothing
continue;
}
//Do something
}
This works perfectly fine but turns out to be a real bottleneck, dropping the fps to a third of what it was. Is there any quicker way? All I'm after is speed, I don't even need to show it on the screen if the comparison is made "behind the scene".
View Replies
View Related
Feb 27, 2015
I have a question in mind that this is my registration form. I am sending these values from HTML form to server and database. I have question that in my case if I click next to Add Another Mobile no in HTML.then a block is genereated and each time a new name is generated.
My Question is if I click 6 times then 6 name attribute are generated. How can I send and differentiate them on my server side.
Because at their I will use something request.getAttribute("Attr_Name");
But they are same. How to do this?.
View Replies
View Related
Jan 23, 2015
how String objects are different from other objects
part 1:
// creating two objects
Dog mydog1 = new Dog();
Dog mydog2 = new Dog();
// comparing the reference variables
if( mydog1 == mydog2){
System.out.println(" The reference variables refer the same object ");
}
else {
System.out.println(" They refer to different objects ");
}
The above code works as I understand objects , it prints "They refer to different objects " to the screen.
Part - 2
// creating two objects ( I beleive, pls correct me if i am wrong )
String a = "haai";
String b = "haai";
if( a == b){
System.out.println(" Reference variables refer to same object");
When i run the above code it prints that a and b refer same object , I don't understand how they refer to same object when i didn't assign " String b = a; ". My question is did java just create one object and stored the same reference values to a and b .
View Replies
View Related
Feb 5, 2014
I have a JScrollPane with two coulmns. In the first column I have an image pane JTable, and in the second a list with names of sections. This second column I try to divide in two columns, one (the second column) to display the names of the sections (each row contains one name), and in the other column (the third) I want to show some values for every section in the row respectively. But, instead of displaying the desired values in the third column, I get the same names of the sections as in the second column.
View Replies
View Related
Nov 8, 2014
I am trying to pass value from class to another using net beans but it seems that I am doing something wrong.
find my basic code at sourceforge.net/projects/val1toval2/files/SRC/
I would like to have both VAL1 and VAL2 having same value in Jlabel.
//Main.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Second SEC = new Second();
l_dis.setText(String.valueOf(getInput()));
l_tot.setText(String.valueOf(SEC.getTotal()));
[code]...
View Replies
View Related
Mar 9, 2015
I was send the id through url like this code.
<td> <%=rs3.getString("Project")%> </td> <td> <a href="TaskAssignment.jsp?id=<%=rs3.getString("id")%>"> <input type="button" name="edit" value="Edit"></a></td>
i have problem to getting this id in servlet.
Servlet:
String id=request.getParameter("id");
the variable id getting only null
View Replies
View Related
Mar 13, 2014
I am having a problem passing an int value from one class to the other. The gist of the objective of this program is to compute a local sum at each client, and then once the local sums are computed.. to compute the overall sum from all the clients. My problem is that I'm having trouble figuring out how to pass each sum from each individual client in the class multicastSenderReceiver to the readThread class. I keep getting an error.
I feel that I have to be able to pass the sum value from each client generated in multicastSenderReceiver to the other class, because that is the only way I'll accurately be able to sum up all the values..
Java Code:
package multicastsenderreceiver;
import java.net.*;
import java.io.*;
import java.util.*;
//import static multicastsenderreceiver.multicastSenderReceiver.numProcesses;
class multicastSenderReceiver{
[code]....
View Replies
View Related
Apr 30, 2014
I'm using Java in BlueJ where I'm trying to write a class to store cycling club membership details in a map. I'm trying to pass a map as a parameter to a function that will iterate over the map and print out the member details. My code is:
public class CtcMembers
{
//instance variables
private Map<String, Set<String>> memberMap;
/**
* Constructor for objects of class CtcMembers
[Code] ....
When I execute the following in the OU workspace:
CtcMembers c = new CtcMembers();
c.addCyclists();
I can see a map populated with the expected details.
When I execute the following in the OU workspace:
c.printMap(c.getMap());
I get the following error:
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
in CtcMembers.printMap(CtcMembers.java:81)
in (OUWorkspace:1)
I wasn't aware I was trying to cast anything so this has got me baffled.
View Replies
View Related