Method not calculating properly

Does anyone know how to set up the command prompt in eclipse? so when you run a program instead of printing out to the console the black command prompt box will appear?
 
I'm not actually sure what you're asking? Are you asking how to setup Eclipse so that instead of writing to the built in console it opens a black Windows command prompt instead, and then runs the Java process inside that? It's been a long time since I used Eclipse properly so I just launched it and had a look. If it's anywhere then I expected it to be in the Run Configuration settings. I can't see any such option so I don't think it's there.

Is that what you wanted to achieve? If so then why do you need to do that? There might be other ways to achieve it such as simply just running the Java program outside of Eclipse.

What are you trying to achieve and why?
 
Are you asking how to setup Eclipse so that instead of writing to the built in console it opens a black Windows command prompt instead,

Thanks for responding Hades.

That's exactly what I'm looking for, there's no particular reason why I need It, it would nice to know how to do it is all, I've tried google but I can't find what I looking for.
 
Sorry for not replying sooner; I've been very busy.

As far as I know Eclipse won't open a command prompt to run Java in. It just uses it's built in console. It would be possible to get your Java program to launch the command prompt and then run another Java program inside that. But I'd need to take a while to work that one out as it's been some time ince I properly used Java.

However, if you wanted to use Eclipse to write Java code and then run it yourself in your own command prompt then that's easy...

Firstly it's useful to understand what Eclipse (or any IDE) is doing when it 'compiles' Java code. It's actually just calling the javac compiler which is part of the Java Development Kit. Javac will take source code and create bytecode in the form of class files (it can also create jar and war files which are a collection of class files, but that's a little more advanced). Let's take this example helloworld code:

Code:
public class helloworld {

    public static void main(String[] args) {
        System.out.println("Hello world");
    }

}


This would be saved as a file called hellowold.java. When you save it in Eclipse it will automatically be compiled to a new file called helloworld.class (you could also do this manually if you install the JDK and simply type javac helloworld.java in the command prompt). Eclipse will save this class file in your workspace directory under a new sub-directoy called bin.

So in order to run that helloworld java program from the command prompt you need to do the following:

1) Save the file in Eclipse so that it creates a class file (or compile it manually using the JDK javac command).
2) Find where Eclipse has created the class file. For example if your Eclispe workspace dircetory is c:\workspaces and your project name is Project1 then Eclipse will probably save it as c:\workspaces\Projet1\bin\hellowworld.class
3) Open a command prompt. Make sure you have Java installed (see below).
4) type cd java c:\workspaces\Project1\bin (or wherever your class file is)
5) type java helloworld (don't add .class on the end, leave that out because java knows it should be a .class)


How to Check If You Have Java Installed

1) Open a command prompt and type java --version
2) If the command can't be found then you need to download java from www.java.com
 
Last edited:
Sorry for not replying sooner; I've been very busy.

As far as I know Eclipse won't open a command prompt to run Java in. It just uses it's built in console. It would be possible to get your Java program to launch the command prompt and then run another Java program inside that. But I'd need to take a while to work that one out as it's been some time ince I properly used Java.

However, if you wanted to use Eclipse to write Java code and then run it yourself in your own command prompt then that's easy...

Firstly it's useful to understand what Eclipse (or any IDE) is doing when it 'compiles' Java code. It's actually just calling the javac compiler which is part of the Java Development Kit. Javac will take source code and create bytecode in the form of class files (it can also create jar and war files which are a collection of class files, but that's a little more advanced). Let's take this example helloworld code:

Code:
public class helloworld {

    public static void main(String[] args) {
        System.out.println("Hello world");
    }

}


This would be saved as a file called hellowold.java. When you save it in Eclipse it will automatically be compiled to a new file called helloworld.class (you could also do this manually if you install the JDK and simply type javac helloworld.java in the command prompt). Eclipse will save this class file in your workspace directory under a new sub-directoy called bin.

So in order to run that helloworld java program from the command prompt you need to do the following:

1) Save the file in Eclipse so that it creates a class file (or compile it manually using the JDK javac command).
2) Find where Eclipse has created the class file. For example if your Eclispe workspace dircetory is c:\workspaces and your project name is Project1 then Eclipse will probably save it as c:\workspaces\Projet1\bin\hellowworld.class
3) Open a command prompt. Make sure you have Java installed (see below).
4) type cd java c:\workspaces\Project1\bin (or wherever your class file is)
5) type java helloworld (don't add .class on the end, leave that out because java knows it should be a .class)


How to Check If You Have Java Installed

1) Open a command prompt and type java --version
2) If the command can't be found then you need to download java from www.java.com


Thanks so much Hades for the effort you put into the above post it very much appreciated :).
 
Hope someone here can help me out, this program prints out employee details and asks the user to enter how many hours they've worked,Run into a bit trouble here, in the method gettotalpay its not returning the any answer but 0, any ideas? thanks.

Code:
public class Employee {

    private String name;
    private int age;
    private String employeeNumber;
    private double amountPerHour;
    private double hoursWorked;
    private double totalWage;
    private static final String MOTTO = "Work Hard Play Hard!";

    public Employee() {

        this("x",0,"xx",0.0,0.0);
    }

    public Employee(String Name){

        this(Name,0,"xx",0.0,0.0);
    }

    public Employee(String Name,int Age){

        this(Name,Age,"xx",0.0,0.0);

    }

    public Employee(String Name,int Age,String EmployeeNumber){

        this(Name,Age,EmployeeNumber,0.0,0.0);
    }

    //public Employee(String Name,int Age,String EmployeeNumber,double AmountPerHour){

   //    this(Name,Age,EmployeeNumber,Age,AmountPerHour,0.0);
   // }

    public Employee(String Name,int Age,String EmployeeNumber,double AmountPerHour,double HoursWorked){

        this.name = Name;
        this.age = Age;
        this.employeeNumber = EmployeeNumber;
        this.amountPerHour = AmountPerHour;
        this.hoursWorked =HoursWorked;

    }

    public static String getMOTTO()
    {
      return Employee.MOTTO;
    }

    public void setName(String value)
    {
       this.name = value;
    }

    public String getName()
    {
        return this.name;
    }

    public void setAge(int value)
    {
        this.age = value;
    }

    public int getAge()
    {
        return this.age;
    }

    public void setEmployeeNumber(String value)
    {
        this.employeeNumber = value;
    }

    public String getEmployeeNumber()
    {
        return this.employeeNumber;
    }

    public void setAmountPerHour(double APH)
    {
        this.amountPerHour = APH;
    }

    public double getAmountPerHour()
    {
        return this.amountPerHour;
    }

    public void setHoursWorked(double HS)
    {
        this.hoursWorked = HS;
    }

    public double getHoursWorked()
    {
      return this.hoursWorked;
    }

    public double gettotalPay()
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the total hours your worked this week:");
        double hoursWorked = sc.nextDouble();

        double totalWage = this.amountPerHour * this.hoursWorked;
        return totalWage;
    }

     public String toString()
     {
        StringBuilder strB = new StringBuilder();

        strB.append(" Name: ");
        strB.append(this.getName() + "\n");
        strB.append(" Age: ");
        strB.append(this.getAge() + "\n");
        strB.append(" EmployeeNumber: ");
        strB.append(this.getEmployeeNumber() + "\n");
        strB.append("Rate Per Hour: ");
        strB.append(this.getAmountPerHour() + "\n");
        strB.append(" Total Wage this week: ");
        strB.append(this.gettotalPay() + "\n");
        return strB.toString();
     }





}


Code:
    new Employee_Tester().tester_Application();

    }

    public void tester_Application()
    {
        Employee s1 = new Employee();
        s1.setName("John");
        s1.setAge(32);
        s1.setEmployeeNumber("112345");
        s1.setAmountPerHour(8.75);
        s1.setHoursWorked(0);

        System.out.println(s1.toString());

        //Employee s2 = new Employee();

        //Employee s3 = new Employee();

        //Employee s4 = new Employee();

        //Employee s5 = new Employee();

        //Employee s6 = new Employee();

        //Employee s7 = new Employee();[/CODE
 
Code:
 public double gettotalPay()
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the total hours your worked this week:");
        double hoursWorked = sc.nextDouble();

        double totalWage = this.amountPerHour * this.hoursWorked;
        return totalWage;
    }


You're assigning the hours worked input to a local variable and then doing nothing with it. I'd assume you meant to do something like this below:

Code:
 public double gettotalPay()
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the total hours your worked this week:");
        this.hoursWorked = sc.nextDouble();

        double totalWage = this.amountPerHour * this.hoursWorked;
        return totalWage;
    }
 
Back
Top Bottom