Regarding the code examples in Head First Java, this is from Chapter 5, regarding the beginning creation of the dot com game. There are two classes in quesiton
the first is the SimpleDotComTester class:
public class SimpleDotComTester {
public static void main(String[] args)
{
SimpleDotCom dot = new SimpleDotCom();
int[] locations = {2, 3, 4};
dot.setLocationCells(locations);
String userGuess = "2";
String result = dot.checkYourself(userGuess);
}
}
and the second one is the code for the checkYourself () method in the SimpleDotCom class
public class SimpleDotCom {
int[] locationCells;
int numOfHits = 0;
public void setLocationCells(int[] locs)
[code]....
Now I noticed that both classes use a variable called result; the program runs fine, but assume from the example that you can use the same variable name two different classes;
The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.
I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.
Java Code: public void myFunction () { int [] myInt; // A local, member variable (because "static" keyword is not there) declared } mh_sh_highlight_all('java');
So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?
when we create another variable and set it equal to the first : Car c2 = c1;
we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car). So if we have code like,
Car c1 = new Car(); Car[] cA = {c1, c1, c1, c1};
are we doing the same? Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)? I think so, but want to make sure I'm understanding this correctly.
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
I have a JFrame jf and JPanel jp on it. jp has five TextFields named cel1, cel2.. cel5. I wish to construct a String Cel + for loop index and run a for loop to reset the values of all the text fields using a single statement such as cel1.SetText("abc"). Similar things can be done in foxfro. How does one do it in java?
I'm quite new to Java. I have some trouble with understanding how to get two classes to get objects from each other (if that is the correct term).
Lets say I have a login class, in which I have a method checking what the user has entered into the console (I have not displayed the whole code, but this class works as it should and give the user an option to enter username and password and return it true if entered correct).
public static boolean validateUserPass(String userName, String password) { String[] user = {"admin"}; String[] passwords = {"firkanten"}; boolean check = false; for (int i = 0; i < user.length; i++) { if (userName.equals(user[i])) { if (password.equals(passwords[i])) { check = true;
Now, in another class I want a display box to appear on the screen and give the user three options: Click yes, no or cancel. I get this to run perfectly alone, this is not the hard part for me. I want the display box only to appear when the correct username and password is given, but I can't seem to figure out how to do this probably.
design a class to conduct a survey of three hospitals. you want to know how many sectors (eg operation, children, gastronomic) each hospitals have, and how many doctors there are in each sector.
I have a Date class and Time class. Is it possible to pass Time object inside Date constructor so that toString function gives output as 12/05/2013 06:31:30 ?
In First class I want to use methods from Second class. So:
Java Code:
Second s = new Second(); s.secondMethod(); mh_sh_highlight_all('java');
Second thing I want to do is use JTextArea from First class in Second class.
So since it gives me error, I extended First class with Second:
Java Code: public class Second extends First { mh_sh_highlight_all('java');
It look like it should work, no errors etc. Also both things are working separately. But since I used both at once...
Java Code:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError at package.Second.<init>(Second.java:7) at package.First.<init>(First.java:17) mh_sh_highlight_all('java');
I can move what I need to First class, and it will work fine, but I want to make this in two classes. But I really don't understand extends, I just use them if there is need for them. So I don't know how to handle this problem.
I also tried to extends Second just like First:
Java Code: public class Second extends JPanel implements ActionListener { mh_sh_highlight_all('java');
Instead of extending First, but it can not be done, since ActionListener is in First...
Well. Also addActionListener can maybe solve my problem?
I have a folder of classes that I am packaging together. Some classes are being packaged and compiling just fine. My other classes in the same package, however, are saying that they cannot find these classes.
What i'm trying to do is to pass a bufferedreader string from main method to a class that will set on how fast or slow a message will be displayed but it has to be displayed on a JTextArea that's in a constructor class.
import java.awt.*; import javax.swing.*; public class demo { JFrame frame = new JFrame ("Idle Game Test!"); JPanel backGroundPanel = new JPanel (); JPanel statusPanel = new JPanel (); JPanel buttonPanel = new JPanel (); JPanel bigPanel = new JPanel (); JTextArea message = new JTextArea ();
So I prefer to use Emacs rather than using Eclipse or some other IDE and I am wondering how to include external classes in my main file?
For instance I have my main file (the one that includes my 'main()' function) and then I have another class (seperate file(s)) for creating a GUI, or making a game and using a Player class, Weapon class, etc. How can this be done? Do I just use 'extends myclass'?
I have been using Guice for a while but just now run into a need to use anonymous inner classes. In the example below Car and its dependencies do not get injected resulted in NPE on consecutive calls. How can this be changed to work properly? Do I need to add extra binding?
public class Truck { @Inject public Truck(Engine engine) { this.engine = engine; } public Car getCar() {
Is it possible to combine two classes that I have defined to contain some of the same elements so that NetBeans stops giving me errors? I don't want to get rid of any necessary code, and if both classes are necessary, should I just rename one of them? One class is an ArrayList that I am using to write the information for employees entered to a text file "employee.txt." I also want users to be able to call on this information via employeeID in order to display employee information. The code is the following:
public ArrayList<Employee> getEmployees() { // if the employees file has already been read, don't read it again if (employees != null) return employees; employees = new ArrayList<>(); if (Files.exists(employeesPath)) // prevent the FileNotFoundException {
[code]....
The other class is a getEmployee class that I previously defined before attempting to read the information from the text file and display it in the console. It is as follows:
private void getEmployees() { try { // if the file doesn't exists, create it if (!Files.exists(employeesPath)) Files.createFile(employeesPath); BufferedReader in = new BufferedReader( new FileReader(employeesFile));
I am new to Java and I am doing an assignment to identify Class and Attributes from below example. How to identify 7 classes and its attributes from this scenario:
ABC Maps Maker produces electronic maps for global positioning systems. Every map needs to define the latitude and longitude of the centre of the map, together with the length and breadth of the map. A map also has a name, and a set of geographical features.
A geographical feature is something noticeable in a a map; e.g., a hill, or valley. Among the types of features are the following: trace features, track features and tract features.
All features have a name that is displayed on the map next to the feature. A trace feature has a coordinate point to indicate its location relative to the centre of the map. Broadcasting stations, mountain peaks, and transmission towers, are examples of trace features. Every trace feature has a description associated with it.
Examples of track features include roads, railways and rivers. Each track feature has a list of points that define its course, and a line pattern. The line pattern specifies the colour, and the thickness.
Like a track feature, a tract feature also has set of points, except that when drawn on the map, the last point is linked to the first point to enclose a complete region. Additionally, it has a fill pattern which incorporates essentially a colour.
Recall that there is a class, Point, in the java.awt package – this can be used to hold the co-ordinate of a point
I am working my way through "Head First Java" and typing the code in the book into Notepad++ as I go. In the first few chapters the code was simple and only had one class (main). Now the code has two or more classes. Originally I would compile the code in the Command Window by typing "javac" and the program's name. After it compiled I would execute the program by typing "java -classpath . " and the program's name. However, now when program has several classes I get the following error: Could not find or load main class. Below is a program I am having issues with... does it need to be saved as two separate files?
class DogTestDrive { public static void main (String [] args) { Dog one = new Dog(); one.size = 70; Dog two = new Dog(); two.size = 8;
I have to make a retail item class that has description, price and quantity. I then have to make a CashRegister class that calculates subtotal, tax, total and displays it (toString). I also have to make a driver class to call the methods from main. I have no clue what I am doing and what I have done does not work. I am trying to make it sort of like a cart, in the CashRegister it should ask the user to enter the description, quantity purchased and the price. Then it would send that to the arraylist of retailitem. I am having trouble using those items in my other methods and then do not know how to call those in the driver class.. So here are my classes:
public class RetailItem { private String description; private int quantity; private double price; public RetailItem(){ description = ""; quantity = 0;