Assigning Private Variables Values From Constructor

Jun 13, 2014

I am able to get output from my constructor when I place a loop inside of it. However when I try to access the private variable that I thought was set by the constructor I get nothing. When I check its size it is zero as well.

Java Code:

public class WinningHand extends PokerCalculator {
private int p1Size;
private int p2Size;
private String[] p1Hand = new String[p1Size];
private String[] p2Hand = new String[p2Size];

[Code] ....

View Replies


ADVERTISEMENT

Calling Out Constructor And Assigning Values To It From A Scanner

Oct 16, 2014

import java.util.Scanner;
public class Try{
static String name;
static int age;
static Scanner a = new Scanner(System.in);
static Scanner b = new Scanner(System.in);
static Scanner c = new Scanner(System.in);
public Try(String name, int age){

[Code] ....

I was trying to make a program that asks the user to create a person or a group of persons and assign names and age to them. So I made a constractor "Try" and a method "AskUser".in AskUser() I use a do/while loop where user is asked to input a name, an age and if he likes to create another person. Now how do I take the input data each time the loop runs, and send it to constractor to create new objects with it? And how these new objects will be called?

View Replies View Related

Private Variables In Class / Constructor

Jan 7, 2014

When creating a class with a constructor, why does one have to create private variables (attributes) to be used as parameters by the object? The object's parameters will be set to be exactly equal to the private variables (attributes), so what is the point of having the private variables (attributes) Why are both private variables (attributes) and parameters needed when they are set to be equal each other anyway?

View Replies View Related

Trying To Initialize Arrays - Assigning Variables

Apr 1, 2014

I'm making a card game and have a class I used to assign all my card variables.

However, it's giving me multiple errors and I can't seem to be able to access the variables from my other classes.

Java Code:

package com.summoners.Screen;
class Cards {
public static String[] names;
name = new String[1000];
atk = new int[1000];

[Code] .....

View Replies View Related

Creating A Constructor Without Private Implementations Only Methods?

Jul 14, 2014

I thought you can only create a new object using private implementations and then using a constructor to set your arguments inside the parameters of the constructor to the instance variables but how come he created an object without any private implementations and just methods inside the constructor.

import javax.swing.JFrame;
public class MyWindow extends JFrame {
public static void main(String[]args){
new MyWindow();
}
public MyWindow(){
setSize(500,500);
setVisible(true);
setTitle("MyWindow");
}
}

View Replies View Related

Does Encapsulation Mean That Variables By Default Need To Be Private

Apr 9, 2015

I just recently started learning about encapsulation, how to set variables private and only accessible to other classes through setters() and getters(). I've seen a couple of programming examples, perhaps not enough but some from here, and I sort of built the assumption that by default all variables need to be private. to make things more clear, here's a card dealer I made which simply

1- generates a fulll deck of 52 cards
2- lets user decide how many players, with 5 as Max number allowed as each player is dealt 10 cards.
3- deal cards

I approached this by making A deck , card , player and game class

import java.util.ArrayList;
public class Deck {
//an Object of this Class would generate a full deck ie an ArrayList of 52 Cards
private String[] suits={"Spades","Diamond","Clubs","Hearts"};
private int[] number={1,2,3,4,5,6,7,8,9,10,11,12,13};
ArrayList<Cards> deck= new ArrayList<Cards>();

[code]....

I can understand why for example Deck class's suit and number arrays are set to private , as they only need to be accessed by this class only. however, both the Deck class's deck arraylist and the Player class arraylist are not private, as I need to transfer these values from one to the other and it felt to me that setting them to private would make it more difficult to deal with them, but what I did instead is to set the Game class dealCard(), which is the only method that have access to them as private. does this achieve the same effect or do I need to set both of these arrayList to private?a follow up question, this is more related to the actual card dealer program, in this code

private void dealCards(){

for(int x = 0 ; x < playerCount ; x++){
for(int y = 0 ; y < 10; y++){
playerList.get(x).pile.add(deck.deck.get(0));
deck.deck.remove(0);
}
}
}

is there an API in ArrayList class that moves(adds to receiver and remove from giver) element across ArrayLists?

View Replies View Related

Object / Methods - Private Instance Variables

Jun 30, 2014

so, i was reading my java book and learning about objects and methods and it starts talking about Encapsulation and mentions that it's good practice to set instance variables as private and instead of accessing the instance variables directly, we should create a set method and get method to get and set the stuff we want to pass to the class containing the object...

for example, in this class, we're passing the integer 70 for object dog one and integer 8 for object dog two for the dog class... and these these 2 integers are sent to the setsize method so we're not accessing instance variable size directly.

i dont quite get it though....if we the programmer are the one deciding what size the integer is for the dog, and the setsize method takes the one.setSize(70) or (8) and puts them in setsize(int s) as s... but only to copy that integer stored in s back to private int size.... why do we even need to bother with making these two extra methods such as setSize, getSize?

in the book it says that... well what if the code gets into the wrong hand and someone writes something like one.setSize(0) then you would get a dog with size 0 which is essentially illogical. but then again, i'm the programmer, and i am the person who writes the code and passing the right integer.The reason for public and private... that part i understand... i can see why if a variable's data can get changed amidst the code during calculations and you dont want it to directly change the original variable and have it mess up the code, but this code from the book just a bad example of demonstrating the reason? since we manually pass the information ourselves and passing it to method setSize... and all setSize does is stores it in another integer, only to copy it right away to size (which is the original private variable we were tryign to protect?

Any simple code to demonstrate how the code might end up changing an instance variable and why we would want to protect it by using private?

class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;

[code]...

View Replies View Related

Splitting Date String By Date And Time And Assigning It To 2 Variables?

Jul 17, 2014

I have an requirement of splitting a Date-Time String i.e. 2013/07/26 07:05:36 As you observe the above string has Date and Time with space in between them.

Now I want just want split the string not by delimiter but by length i.e. after 10th place and then assign it to 2 variable i.e. Date <----2013/07/26 and Time <---07:05:36 separately.

View Replies View Related

Comparison Of Array Values From Other Private Class

May 24, 2014

I got a task from my teacher and the restriction is we are not able to modify this class (and that is the problem).This is the given class:

public class Jobs {
private intcounter= 0;
private final intnoElements= 20;
private final int[]a= { 11, 28, 31, 42, 49, 66, 67, 75, 89, 100, 102, 103, 114, 125, 130, 135, 140, 145, 150, 155 };
private final int[]s= { 20, 9, 7, 6, 12, 15, 4, 7, 30, 22, 11, 45, 20, 6, 6, 5, 5, 5, 5, 5 };

[code]...

I need to compare some of the values of the given arrays. For example: if(a[4]<a[2])... etc.

How can I do these kind of operations to a private array? I have to compare the values in an new classPS: I have to compare the values in a new class

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

How To Display Default Constructor Values

Sep 13, 2014

I am writing a program that accepts input from the user, I want default values displayed before the input values.

Java Code:

public surfboards() {
surfboardType = "Type not assigned";
shaperName = "Shaper not assigned";
constructionType = "Construction Type not assigned";
surboardSize = 0D;
} mh_sh_highlight_all('java');

I can get the output to display as shown below

(THIS IS WHERE I WANT DEFAULT CONSTRUCTOR DISPLAYED. Currently not displayed)

Type not assigned
Shaper not assigned
Construction Type not assigned
0 Feet

(THIS IS WHERE I WANT DEFAULT CONSTRUCTOR DISPLAYED. Currently not displayed)

input:

Java Code:
Enter Surboard Type:
test
Enter Shaper Name:
test
Enter Surfboard Construction Medium:
test
Enter Surfboard Size:
6.5 mh_sh_highlight_all('java');

Output:
test
test
test
6.5 feet

View Replies View Related

Constructor That Creates A New Object With Data From Array Values?

Feb 11, 2015

So I want to write a constructor that creates a new object with the data from the array values. I don't know where to start. It's the last method in the code:

public class Measurements {
private double[] values;
private double[] newArray;
private int n; //numberofvalues
private double[] ms;
public Measurements(int max) { //constructor

[code]....

View Replies View Related

Reading Variables And Their Values From Files?

Apr 13, 2014

So here is my code:

while (key != 3) {
if (key == 2) {
System.out.println("Input from a file");
// System.out.print("Enter file name: ");
// String str = expression.nextLine();
int i = 0;
File file = new File(

[code]....

What I need to happen is the file gets read in, the file is in the form:

a * ( b + c ) / 2 + ( 8 * b )
a = 5
b = 10
c = 20
( x + y ) * z
x = 13
y = 21
z = 3

And so on. My code is supposed to start at the beginning and have output like:

Infix: a * b ( b + c ) / 2 + ( 8 * b )
variable values:
a = 5
b = 10
c = 20

**It then needs to store the variable names along with their values in a hash table**

postfix: a b c + * 2 / 8 b * +
value: 155

And it continues reading the file in the same fashion.

My issue is I am not properly extracting the variables and their values from the file. I have the variables in an array list actually, so I have those, but don't know how to efficiently navigate to the values, then store the character and value in a hash table, while protecting against possible blank lines within the file. Thus just calling nextLine()s and next()s wouldn't work to get to values because they would lead to an error.

View Replies View Related

How To Add Variables And Default Values To Arraylist

Mar 23, 2015

I am facing difficulties in identifying the proper way to add the selected colors of cards into an arraylist. I am having an arraylist which is:

Java Code: private List<String> selectedCars = new ArrayList<String>(); mh_sh_highlight_all('java'); And one more for the carColors:

Java Code: private List<String> carColors = new ArrayList<String>(); mh_sh_highlight_all('java');

The selectedCards array will store the selected cars ['TOYOTA','MAZDA','NISSAN'] as per the selection from the user.For certain types, there is one default color which is 'Black', however for some of them, the user can select different colors.(ex. if selection is 'Toyota')
Java Code:

String carColor="";
String toyotaColor=""; // The value will be retrieved from the form once the user selected the color
if (selectedCars.contains("MAZDA"))
{
carColor="Black";
}
else if (selectedCars.contains("TOYOTA"))
{
carColor=toyotaColor;

[code]....

View Replies View Related

Assigning Random Int To Strings

Jun 16, 2014

while (gamesPlayed<gameQty) {
userChoice = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");
//Determine winner for each game
if (compChoice.equalsIgnoreCase(userChoice));

[Code] ....

It seems to circle through the loop gameQty times, without comparing.

View Replies View Related

Int Declarations - Assigning Length And Width

Mar 19, 2015

What is happening in below mentioned java code. Why in method insert, int l, int w is declaring & it is assigning to length & width.

class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;

[Code] ....

View Replies View Related

JSP :: Assigning Session Variable In Query Syntax

Jan 9, 2015

In my JSP I need to retrieve some data from MySQL. I need to assign a "email" variable to the "WHERE" clause. The variable is retrieved from the session attribute. So far, I have this code:

<sql:query dataSource="${user}" var="result">
SELECT * from users where email = ${sessionScope.email};
</sql:query>

It doesn't work.

View Replies View Related

JDialog - Assigning ActionListener To Buttons And Fields

Apr 27, 2014

I have a program that using one JFrame which opens one of two JDialog windows depending on which button is pressed.

How do I assign listeners to the buttons and fields on the JDialog window? I added listeners on the View end, but how do I process them? I tried adding the '..implements ActionListener" class in the main Controller but it does not recognize/hear anything.

View Replies View Related

Accepting And Assigning User Input Into Multidimensional Array?

Apr 7, 2014

I have been asked to write a library program that will keep record of books and the year it was published. The program should ask the user how many rows he wants accept the string input from the user and display them in rows and columns. This is how i code it

package multidimension;
import java.util.Scanner;
public class bookrecords {
public static void main(String[]args){
//declaring a scanner variable
Scanner input =new Scanner(System.in);

[code]....

View Replies View Related

Assigning Empty Score To Strings In Text File

May 27, 2014

I have a txtfile which I read and go through. My question is what are the ways I could do to read a txtfile of words and assign an empty score to each of the word in it. So each word will have a 0 value. Later on I will manipulate the score but for now I want each word to have a 0 score.What I have at the moment is reading a text file full of words eg:

private void readtextfile() {
try {
Scanner rd = new Scanner(
new File("filename"));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());

[code]...

how to make each of the words to have a score of zero 0?

View Replies View Related

Assigning Int Literal To Byte Works But Not As Method Parameter

Apr 23, 2014

If you write

byte b = 100; it works (implicit conversion of implicit int literal 100 to byte.

But if you have a methodvoid bla(byte b){}

And want to invoke it with a literal (which is an int by default):bla(8) then there is no implicit conversion.

Is the byte b = 100; just an exception in Java? And is it the rule that one has to explicitely cast (narrow) integer literals when passing to smaller-than-int types?

View Replies View Related

Android - Looping Through Parse Data / Assigning To ArrayAdapter

Apr 24, 2015

I am looping through data in Android, using Parse data. I came up with this as a way to get user information; the larger goal is to create a model of data that I can use in an array adapter, so I can create a custom list view (as described here [URL] .... In the example, the data are hard-coded, not pulled from a database.

public static ArrayList<Midwifefirm> getUsers() {
//Parse data to get users
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.orderByAscending(ParseConstants.KEY_PRACTICE_NAME);
query.findInBackground(new FindCallback<ParseUser>() {

[Code] ....

The intention is that for every user that does not have the type patient, collect this data about them, then store it in the arrayList.

On the return statement, though, there is an error: cannot return a value from a method with a void return type.

I may be over complicating this...read through various sources to get a model for this...in the end, I want to display a list of information about specific users, after the user makes a selection of a city...it would therefore display all the information about the medical practices in that city.

View Replies View Related

What Does Static Private Actually Mean

Mar 19, 2015

So I'd like to know what all the statement, not sure if it's the correct way of calling them but I'd like to know what like all of those purple-ish colored words in eclipse actually mean.Here's what I know so far, so if you can add some stuff to it or just correct me:

public - can be accessed by different classes.
private - can't be accessed by different classes.
static - adds a '.' which pretty much lets you like use methods on it? Not really sure about this one.
final - a final value of a variable meaning it couldn't and willn't change?
super - I have no clue, maybe something that has to be executed first? Not sure.
void - bassicly you don't have to use return as it doesn't return any value.
this - Uh-.. I think it has something to do with the class this keyword has been entered into, not quite sure what it does though.

I assume there are alot more but I am just not sure about these common ones, what the actually do and what's their purpose?

View Replies View Related

Using Main As Private?

Mar 25, 2014

What happened when we use main() as a private. . . . .???????

View Replies View Related

Private JTextField Update

Jan 22, 2015

I am creating a slot machine using eclipse. I am trying to get the "winnings" JTextField to be updated in a way so that when the random images have been selected it adds to the number that is already displayed in the JTextField as opposed to what it is doing at the minute which is just displaying how much was won on that particular spin. I am also struggling to set a code for when noting is won, nothing is added. My code is below.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.*;
import javax.swing.Timer;

[Code] ....

View Replies View Related

Using Private ArrayList Of Items?

Apr 29, 2014

It isn't messing up it just keeps making me make my arraylist static and i'm not sure why.

public class driver
{
private static ArrayList<AddItems> items;
public driver()

[Code]....

View Replies View Related







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