Accessing Private From Driver Class
May 14, 2015
I came across the below
1) When a variables are declared "Private" How should it be accessed from the driver class ? Sometimes i get an error in driver class saying "your variable is declared Private" why am I getting this error ...
The document says "Private" declared variables should be accessed only through methods. What does that mean.
View Replies
ADVERTISEMENT
Apr 1, 2013
I have this piece of code I wrote a while ago to test something. The issue is accessing a private field of Base class in Base but of a Derived object.
Here is the code:
class Base
{
private int x;
public int getX()
[Code]....
The commented code does not work but casting d to Base does.
Forgot to mention that the compilation error is that x has private access in Base.
View Replies
View Related
Nov 4, 2014
I am trying to create a method for my "ticketmachine" object that counts ticket objects in an arraylist of "ticket" objects with a specified field "route". The method works if i change the field type of the ticket class to public, however according to the homework task "route" of the ticket class needs to remain a private field.
Here is my code for the method.
public int countTickets(String route) //HAD TO CHANGE Ticket class FIELD "ROUTE" TO PUBLIC!!!!
{
int index = 0; //returns the number of Ticket objects in the machine with the specified "route".
int ticketCounter = 0;
while (index < tickets.size())
[Code] ....
Is my general approach to the problem wrong? also is there a way to access a private field?
View Replies
View Related
Aug 28, 2014
can we pass private final class object to another class constructor?
View Replies
View Related
May 2, 2014
Why we create a driver class?Instead of creating a driver class, if we want to compile our code so will it show output? Let say, we've created a class GradeBook of the institution for students.So they can easily view their profile information and scores in different semesters.so when we have created a class for this purpose, should we create a driver class or not?What is the big advantage of creating a driver class?
View Replies
View Related
Feb 20, 2014
I am having trouble creating a driver for the following program. im new to creating interfaces and i need to make this work.
Lockable interface:
Java Code:
public interface Lockable {
boolean locked();
public void setKey(int key);
public void lock(int key);
public void unlock(int key);
} mh_sh_highlight_all('java');
[code]....
View Replies
View Related
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
May 13, 2014
Assignment:
Create a class; call it Lab4a that will have one method called pull. This method does not return anything and requires no parameters.
-In the method, create three random integers in the range 1 to 7. The method will then display the three numbers to the terminal window.
-Now create a driver program, called SlotMachine, to invoke the pull method of the Lab4a class.
-As a refresher, you will have a main method in the driver class that will create an object of Lab4a and then use the only method of this object.
-In your driver program, invoke the roll method 10 times.
-See the back of this lab for an example of the output.
This is what I have so far.
Slot machine
import java.util.Random;
public class Lab4a {
public static void main (String[]args) {
Random pull=new Random();
[code]....
View Replies
View Related
May 5, 2014
Okay, so I have to create a class with instance data and instance methods.
First, BankAccount class. It should contain the following information, stored in instance variables:
First name: The customer's first name.
Last name: The customer's last name
Balance: The amount of money the customer has in the account.
It should have the following methods:
BankAccount(String firstName, String lastName,
double openingBalance)
This constructor creates a new BankAccount
public String firstName()
Returns the customer's first name
public String lastName()
Returns the customer's last name
public double balance()
Returns the customer's account balance
Finally I need to create a driver to test my class. And create several accounts and verify that the first name, last name, and balance methods work properly. This is my code below.. I don't know if I did it right.
public class BankAccount {
String firstName, lastName;
double balance;
public BankAccount(String firstName, String lastName, double balance) {
[Code] .....
View Replies
View Related
Apr 13, 2015
I am to create a Array class then create a Driver class (TestArray) to test all the methods in the Array Class. Here's the code i've written for the Array Class. I just nee developing the TestArray class.
import java.util.Scanner;
public class Array
{
Scanner sc = new Scanner(System.in);
private double[] array = new double[];
public void setArray(double[] arr) {
[Code] ...
View Replies
View Related
Oct 30, 2014
I have a driver and a main program. How would I go along with calling the encode method to the driver class that I made so I can have the user inputs affected by the encode method?
Java Code:
public class ShiftEncoderDecoder
{
private int shift;
public ShiftEncoderDecoder(int shift)
{
setShift(shift);
}
public int getShift()
[Code] ....
View Replies
View Related
Feb 4, 2015
I want to know is there any way we can call parent class method using child class object without using super keyword in class B in the following program like we can do in c++ by using scoop resolution operator
class A{
public void hello(){
System.out.println("hello");
}
}
class B extends A{
public void hello(){
//super.hello();
System.out.println("hello1");
[code]....
View Replies
View Related
Sep 22, 2014
I'm working on a project that contains multiple classes. Each class contains and must contain only PRIVATE variables. Here's my issue. When my test code calls for a new instance of "StudentClass" as so:
StudentClass studentClass = new StudentClass(offeredClass.getClassIdNumber(),
offeredClass.getClassName(),
offeredClass.getClassroom());
The corresponding constructor won't let me initialize it's variables because they are declared private within another class, as shown here:
StudentClass (float classIdNumber, String className, Classroom room){
this.classIdNumber = classIdNumber;
this.className = className;
this.room = room;
}
When getClassName, getClassroom, and getClassIdNumber are passed to a toString() method elsewhere in my test code. the output is returned just fine. When passed through the StudentClass, I'm getting Null across the board.
View Replies
View Related
Jan 30, 2015
Java Code:
import java.util.Scanner;
import java.util.ArrayList;
public class Problem1
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
[Code] ....
There is an error and says that my ArrayList has private access. I can't figure out how to fix it.
The code runs but when I enter "Quit", the program just stops. The arraylist isn't printed out?
View Replies
View Related
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
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
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
Jul 3, 2014
When we say derived class that means copy of base class plus subclass specific implementations. But when it comes to private members it cannot be accessible in subclass scope. Does it mean byte code generated for subclass doesn't has byte code of private members of super class ?
View Replies
View Related
Oct 10, 2014
I've got an abstract class
public abstract class AbstractClass
{
//stuff
}
And a few classes that inherit from it
public class Class1 extends AbstractClass
{
//stuff
}
public class Class2 extends AbstractClass
{
//stuff
}
within another class I have a private variable with the type of the Abstract class, and within one of the methods I assign an object to the the variable like this:
public class Test
{
private AbstractClass temp;
public testMethod(){
Class1 anObject = new Class1();
temp = anObject;
}
}
Is this legal? Will temp become a Class1 object?
View Replies
View Related
Feb 27, 2015
The String class stores the characters of the string internally as a private char[] and calling someString.length() results in getting the length field from the character array. I am looking to get the details on how the length is implemented. I understand it is a field, but in the original question I provide sample code and really want to know if/how the resulting byte code may differ when compiled, perhaps I am just not seeing the simple answer through my confusion.
Link ....
View Replies
View Related
Mar 23, 2015
If i have a class(lets say class name is Approval) with the following private members: String recip_id, Int accStat, String pDesc, String startDate How can i create public get and setter methods for these private members of the class?
View Replies
View Related
Jun 11, 2014
I am wondering if there is a way in jave to use enums WITHIN a class (without creating a separate enum class) without using private static final. Something like as folows:
class My Class {
myEnum {ACTIVE, INACTIVE, PENDING};
}
is there something like this available?
View Replies
View Related
Feb 9, 2015
Okay, I am supposed to implement the functionalities of the Set class using a private data member of type ListReferencedBased<E>,how the ListReferenceBased works with what I am trying to accomplish.I am trying to complete Set.java, and I have barely started and much of the code doesn't work. ListReferenceBased was given to me completed.
import java.util.Iterator;
pubic class ListReferenceBased<E> implements ListInterface<E>, Iterable<E>{
/** reference to the first element of the list */
private Node<E> head;
/** number of items in list */
private int numItems;
[code]....
View Replies
View Related
Oct 19, 2014
What I want to do is this, this is my first class:
public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
}
(this is just the header of the class... And this is my second class, which contains an ArrayList of the first class:
public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
}
And this is my third class which contains an ArrayList of the second class:
public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
}
What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.
I'm trying this method to get the size of the array in the 2nd class, from the 3rd class (containing an ArrayList of classes of 2nd class, but no luck:
int counter=0;
for(int i=0;i<this.ft.size();i++) {
counter+=this.ft[i].f.size();
}
I'm getting this: Array required, but ArrayList<FootballTeam> found ---
View Replies
View Related
Apr 1, 2014
I have an admin class that needs to access a method of another class and I'm unsure how to do it.
One of the methods in the admin class (DancerAdmin) accesses a .txt file with information in and each line is to be extracted and is to be created as an object of the Dancer class. The information in each line is to then be used to set the variables in the Dancer class.
To set the values the Dancer class has setter methods which I need to access each time a new object is created while cycling through the .txt file. I'm struggling to access these methods from the DancerAdmin class when I run the relevant method.
The snippet of code I have from the method in DancerAdmin is
while (bufferedScanner.hasNextLine()) {
currentLine = bufferedScanner.nextLine();
lineScanner = new Scanner(currentLine);
lineScanner.useDelimiter(",");
dancer.add(new Dancer());
Dancer.setName(lineScanner.next()); mh_sh_highlight_all('java');
I get an error saying non static method setName cannot be referenced from a static content?
View Replies
View Related
Oct 19, 2014
What I want to do is this, this is my first class:
Java Code:
public class Footballer {
int goals;
String surname= "";
String team="";
private static int counter=0;
private int dres;
} mh_sh_highlight_all('java');
(this is just the header of the class, just how it looks)...
And this is my second class, which contains an ArrayList of the first class:
Java Code:
public class FootballTeam{
String teamname="";
String league="";
ArrayList<Footballer> f;
} mh_sh_highlight_all('java');
And this is my third class which contains an ArrayList of the second clas:
Java Code: public class FootballLeague{
String leaguename="";
ArrayList<FootballTeam> ft;
} mh_sh_highlight_all('java');
What I want to do is, know how many of footballers are there in the league? Meaning how many of "f"s are in the "ft"... I remember from C++ it was easy, you just did it something like this: ft.f[i]; (where i is a position), then you'd go through each of them, if you wanted to do something with them, or just ask for it's length, if you needed to know how much footballers are there.
View Replies
View Related