Instances Of Objects In Java Versus Other Languages

May 22, 2015

I am learning JAVA and understanding some of its specific oddities. For example I notice that JAVA loves instances but not variables.

In Python:
x = 1
y = 2
print(x,y)
yields (1,2)

then I do y = x and print
getting (1,1)

This makes sense to me since x and y are variables and thats how variables work. In JAVA I notice this script:

public class TESTRUN {
public static void main(String[] args) {
int x = 0;
int y = x;
System.out.printf("x = %d, y = %d %n", x, y);
x = 9;
System.out.printf("x = %d, y = %d", x, y);
}
}

This yields:
x = 0, y = 0
x = 9, y = 0

So I get that JAVA doesn't understand variables? Or only takes instances of variables? Is there any way to yield a true variable or you always have to update variable relationships?

View Replies


ADVERTISEMENT

Java - Multiple Languages In Dialog Boxes

Oct 22, 2014

My application (e.g., the print dialog box) needs to support multiple languages (user selected either at starting application or by switch in code).

How can this be achieved under Windows 7, Enterprise (works OK under Linux). I have tried : E.g.,

Locale.setDefault( .. );
JComponent.setDefaultLocale(Locale.LANG);
Setting resource bundle

but to no avail. The application always inherit the Win 7 system locale.

View Replies View Related

Networking :: Socket Creation Too Slow Versus URLConnection

Oct 27, 2014

I'm trying to write a transparent proxy like polipo. Polipo is written in C and I want to have the same result in java.
 
A simple program that can filter/monitor all connections created and closed by the browser.
 
To do so, I've chosen to work with sockets, because that's the only way i know to read and write raw data to and from the browser in a completely transparent way.
 
In this moment my code reads and writes every couple of request/response but I've noticed profiling it that the time needed to create the socket is a bottleneck.

Using URLConnection to create the same connection I need much less time than sockets.
 
When socket creation implies 50ms URLConnection implies only 1ms.

View Replies View Related

What Is Meaning Of OpenSource In Any Programming Languages

Dec 29, 2014

what is meaning of Open Source in programming lenguages?

I want to ask this question reagrding this context that if some company or some products that says "This is open source" all codes are available Then it it mean..

1)We can reuse,re produce,distribute this code,modify and sell it for commercial purpose?

I know there is license also attach with a OpenSource.

But What exactly meaning of OpenSource?

View Replies View Related

JavaFX 2.0 :: How To Reload Screen When Changing Languages

Mar 19, 2015

I'm beginner with javaFX and i need to change language of the screen. but how to reload the screen when the language was changed. The application have a button where have the language available. I want just refresh screen when the user change language. Here is the start method to show the stage.

    @Override
    public void start(Stage stage) throws Exception
    {
        this.stage = stage;
        Locale locale = Locale.getDefault();
        ResourceBundle rb = ResourceBundle.getBundle("resources/Label",locale);

[Code] .....

Here is the function to change the language(in the same class with start function),the rb is the new ResourceBundle:

    public void refresh(ResourceBundle rb)
    {
          //change the language here
     }
  
1. I don't want to use the resourceBundle to get value in resource file and set label in scene one by one.like following:

this.btnLabel.setText(rb.getString(key.test));
...
 
2. I don't want to reload the scene,like following:

    public void refresh(ResourceBundle rb)
    {
         try
        {
            loader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"),rb);
            root = (Parent)loader.load();

[Code] .....
 
So do we have a solution to just set the resourceBundle and reload the scene easier?

View Replies View Related

JSP / JSTL :: How To Implement Textarea / Text Box To Accept Multiple Languages

Mar 25, 2013

We have a requirement that we need to have text area/text box on screen to accept multiple languages like (Spanish and russian) that means user can able to enter their text data either in any language like english,russian and spanish(It should be possible to enter the comments in russian and spanish languages). Is it possible to implement in java/j2ee?

View Replies View Related

JSF :: View Scope Versus Application Scope For Beans

Sep 11, 2014

Viewing this example of pagination [URL] and other similar beans for pagination, why do they do these beans view scoped? These beans dont contain any properties for a form so they could be application scoped, right?

View Replies View Related

Instantiate 3 Instances Of Retail Item

Apr 5, 2014

I need instantiating 3 instances of the RetailItem

//Instantiate 3 RetailItems
RetailItem jacket = new RetailItem(); ITS TELLING ME: constructor RetailItem in class RetailItem cannot be applied to given types
jacket= jacket.setDescription("Jacket"); <------- ITS TEllING ME: cannot find symbol
jacket = jacket.setUnitsOnHand(12);
jacket = jacket.setPrice(59.95);

[Code] ....

View Replies View Related

All Instances Of Same Class Change Each Other Private Variable

May 2, 2015

I have a class named Base and a private variable named _hopcount i have 10 instances of class base i use _hopcount as creteria to some if but other instances edit _hopcount so i want to prevent _hopcount edit by other instances; I want to have private variable which other instances of same class can't modify it.

public class Base extends TypedAtomicActor {
private int _hopcount = 0;
if(_hopcount <= 3) {
some code;
}
public function() {
_hopCount += 1;
}
}

View Replies View Related

Static Methods Preventing Usage Of Instances?

Jun 17, 2014

Declaring the method as static precludes one from using any sort of object oriented programming, thus the method cannot access instance fields of the object if it needs to.

I created two short classes to sort of find out what this meant, but I feel I do not understand it well enough.

Test class (main):

package votek; 
public class Test {
 public static void main(String[] args) {
SomeMethod();
}
 public static void SomeMethod() {
Character character = new Character();
character.totalLevel = 50;
 System.out.println(character.totalLevel);

}

Character class:

package votek; 
public class Character {
private int level = 0;
 public Character() {
level = 50;
}
 }

OR

Does it mean that making a static method in the class with private instances will prevent the method from using the private instances?

View Replies View Related

Use Data Read From File To Create Instances?

Sep 10, 2014

"Write a program (save the program as ProductInventory.java) that will read data about 5 products from a text file (Products.txt. The program should use the data read from the file to create instances (objects) of Product.java(Use Product class as a Reference Type)."

I'm yet to discover how I will get the info from the textfile using scanners but I don't know how to "create instances of a reference type".

This is the code of Product.java that came with the exercise.

public class Product {
private String productCode = "";
private String productName = "";
private int quantity;
private String supplierName = "";
private String supplierAddress = "";
private double price;

[Code] .....

By the way, the exercise also required me to use the abstract class ProductLister. To be clearer, the text file is kinda like this:

54643
Rizer Mouse
Mark5s Merchandising
Marquee Mall
4
205.50
23412
Acer Ultrabook
JohnJade Merchandise
Cornal City
2
62200.00
93112
Dell Ultrabook
Octagon Marketing
Cornal City
3
68900.00
22126
Macbook Air Pro
Computerware Services
San Beda, Chad
2
93500.00

View Replies View Related

Driver Program That Creates 2 Instances Of A Class?

May 10, 2014

public class Car
{
//instance variables ----------------------
private String make;
private String model;
private int year;
private double vehiclePrice;
private double downPayment;
private double milesPerGallon;

[code]....

I created this class "Car" (also not sure if it's correct) and need to write a driver program that creates two instances of the class Car. One must use the default constructor, and the other must use the non-default constructor. It must demonstrate the methods used in the Car class using those instances.

public class DriverCar
{
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla", 2013, 20000, 3000, 35);
Car car2 = new Car("Ford", "Taurus", 2005, 14000, 1500, 25);
System.out.println(car1);

[Code] ....

View Replies View Related

Use Applet To Create Two Instances Of Employee Class

Jan 9, 2014

What I have to do: Use Applet to create two instances of the Employee class. Display the data on the Graphics object. Display in the applet the names and values of all of the instance variables in each instance of the class. Also display the value of any static variables.

What I'm doing:

import java.applet.Applet;
import java.awt.*;
public class EmployeeApplet extends Applet {
public static int topSalary = 195000;
int hoursPerWeek;
public static void setTopSalary (int s) {
if (s > topSalary)

[Code]...

I'm not able to display hours per week for e1 and e2.

View Replies View Related

How To Remove / Add Instances Of Class In Linked List

Dec 11, 2014

I have to write a test class for a Contacts class called ContactTest and store the instances of Contacts created into a LinkedList.The ContactTest class should implement the addition andremoval of contacts to the Linked List and display its contents.

So far I have this, which stores the information entered into a Linked List, the problem is I don't know how I go about doing the addition and removal part.

Contacts Class

public class Contacts implements InterfaceContacts {
String fname;
String lname;
String phone;
String email;
//constructors
public Contacts( ){
this("****", "****", "****", "****");

[Code] ....

View Replies View Related

Create Two Instances Of Class And Test Each Of Methods

Nov 1, 2014

How do I use two constructors and I'm having trouble with using char for gender...

Write a program to test the Person class defined below. Your test program should create two instances of the class (each to test a different constructor) and test each of the methods. You are also required to illustrate the error in trying to access private data members from the client class (for clarity temporarily change the private modifier to public and test again). See screenshots below for sample output.

The screen shots are displayed as:

p1 name = Not Given Age = 0 Gender = U
p2 name = Jane Doe Age = 0 Gender = F
p1 name = John Doe Age = 25 Gender = M

and

PersonTester.jave:20: name has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
 PersonTester.jave:20: age has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
  PersonTester.jave:20: gender has private access in Person
System.out.println("p2 name = " + p2.name + "Age = " + p2.age + "Gender = " + p2.gender);
 
3 errors

Here is the class given :

class Person {
// Data Members
private String name; // The name of this person
private int age; // The age of this person
private char gender; // The gender of this person
 
[Code] .....

View Replies View Related

Creating Instances To Represent Deck Of Cards

Feb 12, 2014

I am creating a deck of 52 cards each with their rank(1 2 3...King, Ace) and suit(Clubs... Spades).

I want to be able to print out value of each card after creating them such as - 2 of CLUBS and so on. I am able to do that now. Only problem is, when it comes to the picture cards, I am representing them with numbers 11(Jack), 12(Queen) 13(King) and 14(Ace). I can't quite get around to changing the numbers to be represented as the actual words (King and so on).

I imagine that I should store the jack,queen,king,ace values in an enum too and call it in my loop but can't quite get it. Should I have like a 3rd for loop in my following codes to make it work?

Java Code:

//My main method. The Card class follows below

public static void main(String[] args) {
Card[] cards = new Card[52];
int i = 0;
for (Card.Suits suit : Card.Suits.values()) {
for (int y = 2; y < 15; y++) {
cards[i] = new Card(y, suit.name());
i++;

[Code] .....

View Replies View Related

Which Iterator To Be Use To Create Instances From Feature Value Pairs (Mallet API)

Mar 12, 2015

I am tring to run LDA to generate some topics from txt files as the following one:

Document1 label1 forest=3.4 tree=5 wood=2.85 hammer=1 colour=1 leaf=1.5
Document2 label2 forest=10 tree=5 wood=2.75 hammer=1 colour=4 leaf=1
Document3 label3 forest=19 tree=0.90 wood=2 hammer=2 colour=9 leaf=4.3
Document4 label4 forest=4 tree=5 wood=10 hammer=1 colour=6 leaf=3

Each numeric value in the file is an indication of the number of occurrences of each feature (e.g., forest, tree) multiplied by a given penalty. To generate instances from such a file, I use the following Java code:

String lineRegex = "^(S*)[s,]*(S*)[s,]*(.*)$";
String dataRegex = "[p{L}([0-9]*.[0-9]+|[0-9]+)_=]+";
InstanceList generateInstances(String dataPath) throws UnsupportedEncodingException, FileNotFoundException {
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
pipeList.add(new Target2Label());

[Code] .....

I then add the so-generated instances to my model using the instruction model.addInstances(generatedInstances). The resulting output is described below.

It contains errors caused by the instruction model.addInstances(generatedInstances). Debugging my code showed me that the alphabet associated to the model is null. Am I using the wrong iterator?

name: document1
target: label1
input: TokenSequence [forest=3.4 feature(forest)=3.4 span[0..10], tree=5 feature(tree)=5.0 span[11..17], wood=2.85 feature(wood)=2.85 span[18..27], hammer=1 feature(hammer)=1.0 span[28..36], colour=1 feature(colour)=1.0 span[37..45], leaf=1.5 feature(leaf)=1.5 span[46..54]]
Token#0:forest=3.4 feature(forest)=3.4 span[0..10]

[Code] ....

View Replies View Related

Switch Statements And Multiple Instances Of User Input

Sep 25, 2014

Ok, so I've seen alot of things online about writing switch code, but I can't find any involving user input, let alone multiple input scanners.

The goal is I let the user put in two different integers and it will decide based on both of the integers which of the 15 possible results it will display.

I am also not allowed to use if/else statements in this program.

View Replies View Related

Reading Numerical Values From A File And Saving All Instances

Mar 8, 2014

My code's not working and I don't know why. I'm trying to read numerical values from a file and saving all instances where a letter is entered instead of a number to a string to be referenced as an error at a later point on in the code. However, there's an error and like I've stated before, I don't know what caused it.

public static void validateData() throws IOException{
File myfile = new File("gradeInput.txt");
Scanner inputFile = new Scanner(myfile);
for (int i=0; i<33; i++){
if(inputFile.hasNextDouble()){
double d = inputFile.nextDouble();
if (d<0||d>99999){

[Code] ....

This is the error that returns.

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Paniagua_Grading.validateData(Paniagua_Grading.java:29)
at Paniagua_Grading.main(Paniagua_Grading.java:6)

View Replies View Related

How To Find Multiple Instances Of Same Letter In A Single String

Nov 29, 2014

I have got a pretty good framework working for my hangman game so far, however I am quite stumped on how to find multiple instances of the same letter in a single string. I am using indexOf to find the instance of a character in a string, but it only returns the first instance. So if the words(s) contain(s) multiple instances it doesn't register the rest.

public static void main(String[] args) {
String word = "crazy horse";
String answer="";
String space=" ";
String dash="-";

[code]....

View Replies View Related

Counting Repeated Instances - Parallel Array Is Not Working Correctly

Sep 6, 2014

So in my parallel array i read from a textfile of strings and if i enter the string into the string array and if strings are repeated i store it in a parallel array that counts repeated instances. I'm supposed to get 27 15 21 23 20 but instead i get 106 0 0 0 0....

Scanner sc=new Scanner(new File("Cat.txt"));
String category=sc.nextLine();
int total=sc.nextInt();
int[]totcat=new int[total];
String[]names=new String[total];
while(sc.hasNextLine()){

[Code] .....

View Replies View Related

JavaFX 2.0 :: Why Does Scene Builder Constantly Create New Instances Of Nodes

Nov 28, 2014

I thought I had a simple idea for creating a control that would let me get some of the behavior of a card pane.  This is the entire control:
 
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;

[Code] ....
 
The idea is pretty simple; extend StackPane, add an active property, bind the visible and managed properties of the pane to the active property, and, whenever the active property is changed to true, iterate sibling nodes de-activating any siblings that are also of the type Card.
 
However, this doesn't work with Scene Builder.  While trying to debug, I created an ExtStackPane:
 
import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
public class ExtStackPane extends StackPane {
    {
        getChildren().addListener((ListChangeListener<Node>) c -> {
            System.out.println("ExtStackPane children change: " + c.toString());
        });
    }
}
 
All this does is log list change events.  However, I was very surprised by the output when working in Scene Builder.  I added both controls to Scene Builder and did the following:
 
0) Added an ExtStackPane
1) Added a Card to the ExtStackPane
2) Added another Card to the ExtStackPane
3) Added a Label to the first Card
4) Added a Label to the second Card
5) Changed the text of the first Label to Hello
6) Changed the text of the second Label to World
7) Set the first Card to active
8) Set the second Card to active
 
I get the following output:
 
1)
ExtStackPane children change: { [Card@5b9067b3] added at 0 }

2)
ExtStackPane children change: { [Card@6b6328bd] added at 0 }
ExtStackPane children change: { [Card@6aca8cc5] added at 1 }

[Code] ....
 
This is what things look like in Scene Builder:
 
Does Scene Builder recreate the entire hierarchy every time I make a small change?  Here's an application that does the same as the manual steps I performed in Scene Builder:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class CardApplication extends Application {

[Code] ....

The output when running the above is:

1)
ExtStackPane children change: { [Card@6dfaa767] added at 0 }

2)
ExtStackPane children change: { [Card@6aa2c411] added at 1 }

[Code] ....
 
The behavior is obviously a lot different than when I'm working with the control in Scene Builder. What Scene Builder is doing to change the behavior of my Card control so much?  Does my Card control break some rule(s) I'm not aware of?

View Replies View Related

Java Lifetime Of Objects

Nov 16, 2014

how the Java-compiler manages the lifetime of objects. Aren't the variables a, b and, therefore, the objects A(i),B(i) destroyed after each iteration of the loop? Why then does the toString-method print out A(4), B(4) after finishing the for-loop?

Code:

public class mainClass {
public static void main(String[] args) {
C a = new A(0);
C b = new B(0);
for(int i=0;i<5;i++){
a = new A(i);
b = new B(i);
}
System.out.println(a+" "+b);
}
}

View Replies View Related

Searching Array Of Objects In Java

Jul 23, 2014

'm working on a program that is to act as an inventory for a book store. There are two classes (Store and Book), and numerous methods which you will see in my code. The program is supposed to read in an inventory from a file which contains 10 books (each row in the file represents a book with an ISBN number, the price of the book, and the number in stock)and store this file into an array of type Book[]. There is then to be another method to process a purchase interactively. The Store class is also to keep a count of how many books were sold at the time of closing and also how much was made that day.

I'm having some difficulty figuring out how I should set up the purchase processing method though. I've written out how I think it would be set up (not too familiar with the terminology, but I think it would be called pseudo code?) I'll include that now along with a few other notes.

public Book[] purchase(String isbn, double price, int copies) {
int itemsSold;
double totalMade;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the ISBN number of the book you would like to purchase: ");

[Code] ....

So basically here's what needs to be accomplished with this portion of code:

1.Ask the user to enter the ISBN number of the book they'd like to purchase.
2.Search the array for the object that contains that ISBN.
3.If the ISBN isn't found in the array, display a message stating that we don't have that book.
4.If the ISBN is found but the number of copies is 0, display a message saying the book is out of stock.
5.If the ISBN is found and the number of copies is greater than 0, ask the user how many copies they'd like to purchase.
6.If the number they enter is greater than the number of copies of that book in the array, display a message stating that and ask them to enter another quantity.
7.Once the purchase is complete I need to update the array by subtracting the number of copies of that particular book that was purchased.
8.Print the updated array.
9.Display a count of how many books were purchased, and how much money was made from the purchase.

I know that code has a lot of holes in it, but I'm just trying to get everything together and figure out how to do each step.

The part I'm stuck on right now is trying to figure out how to search the array to first see if the ISBN that was entered is in the array, and then to find the number of copies of the book with that ISBN number.

In case it will visualize what this array looks like, here's a print out of the array from a sample run I just did.

ISBN: 1234567, Price: 31.670000, Copies: 0
ISBN: 1234444, Price: 98.500000, Copies: 4
ISBN: 1235555, Price: 27.890000, Copies: 2
ISBN: 1235566, Price: 102.390000, Copies: 6

[Code] ....

View Replies View Related

How To Move Objects In Java Using Buttons

Aug 17, 2014

I have a program which require to navigate a circle with for buttons, Up, Down, Right, and Left. How can i do that?

Here's my code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package moveobject;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MoveObject extends Applet implements ActionListener

[Code] .....

View Replies View Related

Comparing 100 Objects Using Comparator Interface In Java?

Jul 4, 2014

Is it possible to compare 100 objects using comparator interface in java?

View Replies View Related







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