class box {
double ht,wdt,len;
box(double h,double w,double l) {
ht=h;
wdt=w;
len=l;
}
double volume() {
return ht*wdt*len;
}
}
class boxme {
public static void main(String args[]) {
box mybox= new box(1,2,3);
System.out.print("The volume is "+mybox.volume());
}
}
// For this code to be run in bluej,i still need to give the arguments after the object creation (though I have already given them in my code).The same code works well in cmd but shows this difference when attempted in bluej.Please provide a reason and solution to bring out an equivalence between bluej and cmd?? //
When you have two different classes and you want to use the methods in the other class, you have to create an instance of that class.
Right-click on the second class and run the public static void main(String args[]) function.
And please note the name of the class must start with an upper case letter and the fields must be private scoped for security, Objects should be always in lower case.
public class Box {
private double ht,wdt,len;
public Box(double h,double w,double l) {
ht=h;
wdt=w;
len=l;
}
public double volume() {
return ht*wdt*len;
}
}
public class boxme {
public static void main(String args[]) {
Box mybox= new Box(1,2,3);
System.out.print("The volume is "+mybox.volume());
}
}
You don't need to create an object while running it in BlueJ explicitly, since you already have the main function defined.
Right click on the class and run the public static void main(String args[])
function.
Related
I've just started learning about constructors and know that they're used to initialize objects. But i noticed that objects can be given initial values in the declaration (as in example 2) without having to specify them separately in a constructor.
Both examples below give the same result so what is the benefit of first declaring the attribute and then specifying the value in a constructor instead of right away in the declaration?
Example 1:
**public class Main {
int x;
public Main() {
x = 5;
}**
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Example 2:
**public class Main {
int x = 5;**
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
In your example, where you always want to set the value to a pre-defined value, a constructor is not required. It can be initialized at the time the attribute is defined. However, in most cases, it is not so simple - value of a class attribute is usually passed in at the time an instance is created. In such cases, a constructor is a good way to initialize the attributes of a class.
public class Main {
int x;
public Main(int x) {
this.x = x;
}
public static void main(String[] args) {
Main myObj = new Main(6);
System.out.println(myObj.x);
}
}
I have two different files. In the file below I create a new object with an attribute and I create a method to retrieve that attribute.
public class Journey
{
public double singleCost;
public void setSingleCost(double cost) {
singleCost = cost;
}
public double getSingleCost() {
return singleCost;
}
public static void main (String[] args) {
Journey leicester_loughborough = new Journey();
leicester_loughborough.setSingleCost(2.5);
Journey leicester_nottingham = new Journey();
leicester_nottingham.setSingleCost(3.5);
}
}
In a separate file, I would like to print out that object's attribute. I'm not sure how to do that though. The following doesn't seem to work.
public class JourneyMethods
{
public static void main (String[] args) {
System.out.println(leicester_loughborough.getSingleCost());
}
}
I would appreciate any help, thanks.
You are thinking about this too much as main methods.
Your JourneyMethods main method knows nothing about the Journey class unless you tell it. JourneyMethods will be able to interact with a Journey instance if you create a reference to a Journey.
Your class object :
public class Journey
{
public double singleCost;
public void setSingleCost(double cost) {
singleCost = cost;
}
public double getSingleCost() {
return singleCost;
}
}
Your main class :
public class JourneyMethods
{
public static void main (String[] args) {
Journey Leicester_loughborough = new Journey();
leicester_loughborough.setSingleCost(2.5);
Journey leicester_nottingham = new Journey();
leicester_nottingham.setSingleCost(3.5);
System.out.println(leicester_loughborough.getSingleCost());
}
}
This is how Java works, I think that you are confused with C (with .h and .c files)
Having a main() function in your Journey class does not make sense, you can instantiate your object from anywhere in your code, so have it in your main() function :)
In Java, you generally want to put methods in the Object class.
You simply create a getter()-method for the attribute you want to have in the class that has it (in your case Journey.class).
Like this:
public double getSingleCost() {
return singleCost;
}
You can then get the attribute by calling the getSingleCost() from the object-instance you have. Like this:
double cost = leicester_loughborough.getSingleCost();
Please note that this is really basic stuff and you might want to read some more Java tutorials first.
Oh, and your program is only supposed to have one main() method as entry point. (there are reasons to have more than one but those are very case-specific). If you run your program in the way it's written in the OP, the address spaces as well as the execution of the two applications are seperated from each other and you do not have a means to access objects that are contained in a different application (well, there are ways like serialization, but those are more advanced topics).
Have your classes/objects contained in the same program.
Edit: Fixing OP's code to work as intended:
public class Journey
{
public double singleCost;
public void setSingleCost(double cost) {
singleCost = cost;
}
public double getSingleCost() {
return singleCost;
}
}
JourneyMethods:
public class JourneyMethods
{
public static void main (String[] args) {
Journey leicester_loughborough = new Journey();
leicester_loughborough.setSingleCost(3.5);
System.out.println(leicester_loughborough.getSingleCost());
}
}
You need to create the two journey objects in your JourneyMethods class so you have a reference to them. Also you should refer to the 'files' as classes, because 'file' usually implies files on the filesystem, not files representing Java classes. That confused me a bit.
To access the object attributes, you have to make the objects in the external class and use getter and setter methods to get the values of the objects.
public class Journey{
public double singleCost;
public void setSingleCost(double cost) {
singleCost = cost;
}
public double getSingleCost() {
return singleCost;
}
}
public class JourneyMethods{
public static void main (String[] args) {
Journey Leicester_loughborough = new Journey();
leicester_loughborough.setSingleCost(2.5);
Journey leicester_nottingham = new Journey();
leicester_nottingham.setSingleCost(3.5);
System.out.println(leicester_loughborough.getSingleCost());
}
}
I am using the BlueJ IDE. I have a main class entitled ProgramOne, and another class StarTurtle (intended to serve an instance method).
Here is the code of ProgramOne:
public class ProgramOne
{
public static void main (String[ ] args) {
StarTurtle turtle1 = new StarTurtle();
int result = turtle1.StartTurtle(5);
}
}
Here is the code of StarTurtle:
public class StarTurtle
{
private int points;
public int StartTurtle(int x)
{
points = x;
Turtle sue;
sue = new Turtle();
sue.paint (90, 40);
}
}
(The turtle method you see is from two other classes that I have not pasted here for the sake of brevity. These classes are found in the http://www.cs.ccsu.edu/~jones/book.htm manual)
The code only compiles, and there is no option to execute. However, there is no option to execute void main (String[ ] args), which there should be to execute the main class. Does anyone know what is the cause of this? I am assuming that there is a problem in the code itself. The StarTurtle class does execute, but the main class ProgramOne does not, which leads me to believe that the problem lies in the ProgramOne class.
When I mean "option to execute", I am referring to this BlueJ functionality:
When you call the main method from the class ProgramOne, you have created an instance of StarTurtle turtle1. But when you assign value in result variable by calling turtle1.StartTurtle(5) method, nothing is stored in that variable. The problem is that you have issue in this function in your StarTurtle class. You have defined public int StartTurtle(int x) function as return type but actually it is not returning any thing. Therefore, you need to add a return statement on that block of code.
public class StarTurtle {
private int points;
public int StartTurtle(int x){
points = x;
Turtle sue;
sue = new Turtle();
sue.paint (90, 40);
return points;
}
}
However, on the other hand, your class and functions are incorrect though they are working. You classes should be like this.
public class ProgramOne{
public static void main (String[ ] args) {
StarTurtle turtle1 = new StarTurtle();
turtle1.StartTurtle();
}
}
public class StarTurtle {
//Void type of method instead of previous return type
public void StartTurtle(){
Turtle sue;
sue = new Turtle();
sue.paint (900, 550);
}
}
This program must print final parametres about a TV state in three (3) rows...
in first row...on which level loudness is...
in the second on which program is (1., 2., 56, etc...)
third row - is it turned on...
(after all operations inside programm)... this programm is pasted from manual for Java...
class Television {
int volumeTone = 0;
int channelNow = 1;
boolean turnedOn = false;
void turnOn(){
turnedOn = true;
}
void turnOff(){
turnedOn = false;
}
void increaseVolume(){
volumeTone = volumeTone + 1;
}
void decreaseVolume(){
volumeTone = volumeTone - 1;
}
void turnOffVolume(){
volumeTone = 0;
}
void changeChannelUp(){
channelNow = channelNow + 1;
}
void changeChannelDown(){
channelNow = channelNow - 1;
}
int returnChannelBefore(){
return channelNow;
}
int returnToneVolume(){
return volumeTone;
}
boolean isItTurnedOn(){
return turnedOn;
}
void writeParametres(){
System.out.println("Volume loudness now is "+volumeTone);
System.out.println("Channel now is "+channelNow);
System.out.println("Tv is turned on? "+turnedOn);
}
}
You need to have a main method in the class to run...
Your exception itself say that, please define the main method as:public static void main(String[] args)
You need main method in your class Television as shown below.
public static void main(String[] args)
{
//Your logic run this class
}
In Java, you need to have a method named main in at least one class.
Please refer this link about main method.
Without a main method, you will not be able to run anything. Create a new class with a main method like so:
public class myClass
{
public static void main(String [] args)
{
//Here you can create an instance of your television class
Television tel = new Television();
//You can then call methods on your newly created instance
tel.turnOn();
}
}
The main entrance to a java application is with a static main method
which is usually defined as;
public static void main(String []args)
{
}
If you want to run any class make sure it has this method.
You will need to Implement main method
From The Java Tutorials' Hello World Example: (emphasis mine)
In the Java programming language, every application must contain a main method whose signature is: public static void main(String[] args)
...
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
In other words, when running a Java class, the JVM will look for a main() method in it. That method will be called automatically when running the class, and will be the entry point for the execution flow of the program.
Also, take a look at How does the main method work?.
I've been using Eclipse for running Java Programs. Everything was going well earlier but now I'm unable to get the option "1 Java Application" when I click on "Run as" despite having zero errors in my program. Could anyone help me how to deal with it ?
class Base{
public int baseVar;
public int var;
public Base(int v){
baseVar=v;
System.out.println("Base class parameterized constructor");
}
}
class Der extends Base{
public int derVar;
public int var;
public Der(int v){
super(v);
derVar=v;
System.out.println("Derived class parameterized constructor");
}
public void display(){
System.out.println("Base variable value="+baseVar);
System.out.println("Derived variable value="+derVar);
}
public void useOfSuper(){
var=15;
var=20;
System.out.println("Base variable var=" +
super.var);
System.out.println("Derived variable var="+var);
}
}
class abc{
public static void main(String args[]){
Der Derobj=new Der(10);
Derobj.display();
Derobj.useOfSuper();
}
}
I suspect your program doesn't have an appropriate main method:
public static void main(String[] args)
If it does, try running it from the command line - does that work?
EDIT: As noted in comments, whether or not the presence of a main method affects the context menu appears to depend on the version of Eclipse. In the version I'm using at home (4.2.1) the context menu option doesn't appear unless there's a main method.
Change :
class abc
with
public class abc
I suspect that the class is private, hence you can't run it. You have to change your java name file to abc.java and make the class abc public.
Please use below. Your code is missing public identifier for class that contains main method.
public class abc {
public static void main(String args[]) {
Der Derobj = new Der(10);
Derobj.display();
Derobj.useOfSuper();
}
Your main() is in your class abc, move it into your Base and things will work (I am assuming here that all of the code you published has been placed in a single file named Base.java)