I need to create a Dog class that represents a dog. Then, there are 2 different methods that in the end will each represent one dog each. Each printout will show the Name, Breed, Age, and Age in Human Years. Right now I'm having trouble trying to move methods in between each class. Here's the first class.
import java.util.Scanner; public class Dog { //no main method static Scanner input = new Scanner(System.in); //writeObject method public static void writeObject(String name, String breed, int age) { System.out.println("Enter the dog's name, breed, and age: ");
how come you can call non static methods from other classes(objects when they are created from main) but not static methods in the same class as the main method??
example I cannot call the method maximum from the main method aslong as its not static BUT i can call other objects non static methods from main??
class test{ public static void main(String [] args){ Scanner input = new Scanner(System.in); //create new Scanner object //for input int number1; int number2;
I have a very standard Lab assignment. It's probably been seen a lot. I wrote the first part not realizing I had to write a second class to do use the methods. I'm not sure how to change my program to call methods from my second class instead of doing all my calculations with user input in my first class.
Here's the first class' code:
package tickets; //Imports classes used for "Ticket" application. import java.util.*; import java.text.DecimalFormat; public class Tickets {
[Code] ......
The code is obviously incomplete. I have not tried to compile, nor would I expect it to compile right. I'm not sure how to move my calculations from the first class shown above into my second class and use them as methods.
Basically I want to make a class called library, but I don't want to make an interface because I actually want to define the methods. I think I can only use abstract classes but not really sure how to use those. But I still have a problem, I want to create a Map that classes implementing Library class have to have in their code, and the Map will be a HashMap with <String, ParentClassHere>, so basically let's say I make a class called Car, implemeing Library to the Car class would create a Map library = new HashMap<String, Car>. Can I do something like this? And also include methods to get values and set values to the library Map?
I have a javafx class that has buttons and textfields. Here is the code for a button, and i want the button to make a new object but im having trouble setting the constructor
Label label = new Label("Type"); GridPane.setHalignment(label, HPos.RIGHT); TextField textField = new TextField();
Label label2 = new Label("First Name"); GridPane.setHalignment(label2, HPos.RIGHT); TextField textField2 = new TextField();
[Code] ....
after i create the object i will insert the object in an arraylist of person objects
I am writing a game in Java for Android (although my question isn't Android or Game Dev specific).
I have a SceneManager class and a Scene interface and then various other classes that implement the Scene interface (Code at the end of this post).
Basically, in my MainGame class (which also implements the Scene Interface for Touch Event capturing purposes) I hold the bulk of my game code. Methods in this class are then called from my Level classes. (most of these are needed in all levels so it makes sense to hold them here and call them from the levels to eliminate unnecessary code duplication)
So, I have Level1, Level2......... Level20 classes which all implement Scene.
Now, the problem comes because in only 2 of my Levels something can happen (that can't in the other 18) and I need to run a response method in these 2 levels (the method isn't exactly the same, the response to this event happening is different for both levels).
To run common methods from my classes, I use my Scene Manager like this:
This works great as all Level's have an updateLogic(); and render(); method.
So from my mainGame class, I am doing something like : (pseudo code)
public void checkIfSomethingHappened(){ if (something happens){ if (currentLevel==5){ Level5.response();}
[Code]....
The above would be called from my 2 level classes. So something like:
MainGame.checkIfSomethingHappened(); //Called in addition to the normal methods that make up that level
I don't really want to have this (second) 'if' statement here in the middle of my performance critical game loop.
What I'm after is something like this:
if (something happens){ SceneManager.getInstance().getCurrentScene().response(); }
However, this would require me to put stubs in the other 18 classes.
I'm thinking there must be a way to do this as the SceneManager already knows the current scene so it seems a waste checking it again via an if (or switch) statement. What is the best way to do this without having to put stubs into classes that don't require this method?
i have this following codes and im trying to make a java program that calls an object value initialized.
public class TestClass { public String name; public int age; public void myMessage() { System.out.println("Hello I'm" + name + "and i'm" + age + "years old"); } }
what i want is to put the following codes of the object.. so once i compile and run the project it executes the value of an object from the method i defined above.
Why do I see many exceptions for one missing file on the stack trace ? My guess is, where ever that file or methof is being called , all of them will throw exceptions. So, where do I find the root exception, first one which is thrown OR the last one ?
java.io.FileNotFoundException: c: emppw.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:120) at java.util.Scanner.<init>(Scanner.java:636) at com.rbc.ReportDriverRunner.getPassword(AuditReportDriverRunner.java:39)
[Code] ....
In the above stack trace, "java.io.FileNotFoundException: c: emppw.txt (The system cannot find the file specified)" is the root cause (Assuming there is only one error which causes code to fail) ? Is the first one cause of failure whch then cascades failure of other methods ?
I've written a java application with several classes all in the same .java file. It works just fine. Now, I've broken it up so that each class has its own .java file. Still works fine. My next step is to put those classes into a package, but I'm not about to get the program to run.The .java source files are all in /home/user/src
I've set the CLASSPATH to /home/user/src..All of the source files have "package com.myfirm.program" on the first line.I compiled the application with:
the compiler created the directory: /home/user/src/com/myfirm/program and put all of the .class files in there.So how do I get the program to run? if I run from /home/usr/src
java File1
I get: Exception in thread "main" java.lang.NoClassDefFoundError: File1 (wrong name: com/myfirm/program/Program)
What is exactly reason for existence of inner classes? Are there problems that without them you can not (or be very hard to) resolve? Anything beside emulate multiple inheritance, when you need to extends the real classes ( not implements multiple interfaces) ?
why don't I define my methods in a class, rather than going a level up and declaring it first in an abstract class/interface? If the point is to have different implementations for different needs, then we have the option to override the methods.
1) when you create a class and compile it and open and see the class using javap command .. that class file contains that it extends java.lang.object and a default constructor is created automatically..... But when you extends another class the java.lang.Object class is not appeared as extended why?
Code example:
1St Case ----- Java code:
public class temp(){ }
Javap Code: public class temp() extends java.lang.Object { public temp(){ } }
2nd case --- Java Code: public class temp() extends dummy{ }
javap Code:
public class temp() extends dummy{ public temp(){ } }
why for the above scenario it doesn't extends object class .. if it does implicitly then why it did not do in the first case instead why did the compiler extends Object class ?
I was wondering how Java knows where to Import classes from when we don't specify the whole directory path.
For example: import java.util.* Does It automatically search In both your current directory and the directory created during Installation that contains the standard library (wherever It may have been Installed) ?
I studied that java does not support multiple inheritance using classes. It is fine practically, but I have still a question in mind:Lets see the below code.
class Test{ ------ ------ }
class MyTest extends Test{ --------- --------- }
Here, as we know that that Object is Super class for every class in java. and I have already extends a Test class in MyTest.My question is: How is it possible to extend features of two class at the same time?
How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in
If i have 2 classes, Top and ClassB which extends Top
public class Top { String variable; public Top(){ setVariable(); } void setVariable(){ variable = "variable is initialized in Main Class";
[code]....
So what is happening when ClassB inherits from Top?I know that the B constructor is calling super, so does that mean its calling setVariable (in Top?) but as its overridden in ClassB, then that is whats being called and setting the String variable?
The question states: Design a class named LinearEquation
for a 2 x 2 system of linear equations:
ax + by = e
cx + dy = f
Where
x =
ed − bf/ad − bc
y =
af − ec/ad − bc
The class contains:
- Private data fields a, b, c, d, e, and f. - A constructor with the arguments for a, b, c, d, e, and f. - Six get methods for a, b, c, d, e, and f. - A method named isSolvable() that returns true if ad−bc is not 0. - Methods getX() and getY() that return the solution for the equation.
Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad − bc is 0, report that "The equation has no solution."
I believe that my program is correct because I am able to compile it and get no errors, however I have no clue how to display the information for x and y or display this equation has no solution if ad-bc=0.
Java Code:
import java.util.Scanner; public class Exer911 { public static void main(String[] args){ // Create a scanner system to hold the numbers for each variable Scanner input = new Scanner(System.in); // Prompt the user to enter a number for each of the variables
I use jboss eap 6.2 under eclipse.I perform a tutorial on EJB and I want to show the difference between a stateless session bean and a statefull session bean. I want demonstrate that the first one keep its state (if it has one) but that 2 successive don't send back necessarily the same SLSB : it send back the SLSB that is ready in the pool. But I don't suceed in obtaining 2 different SLSB by 2 successive call. The system always send me back the same SLSB.
I am trying to calculate a fine in a PoliceOfficer object with method calls to a ParkedCar and ParkingMeter object. The word problem is:
The fine is $25 for the first hour or part of it and $20 for every additional hour of part of it.
My code is:
public class PoliceOfficer public static final int PARKING_FINE1 = 25; public static final int PARKING_FINE2 = 20; public static final int NUMBER_OF_MINUTES_OVER_PARKED = 60; public double calculateFine(){ double calculateFine = 0;
[Code] ....
obviously the fine is not calculated correctly but I'm not sure how to proceed from here...