Java – constructor problems?

Java – constructor problems?
I am a beginner in Java programming and have run into this problem more than once. I am trying to write code for a class Microwave that has MicrowaveTest as well. My main problem right now is the constructor; here is the error message for it…

symbol : constructor Microwave(double,int,java.lang.String,java.lang.String)
location: class Microwave
Microwave myMicrowave = new Microwave (7.0, 9, “off”, “off” );
1 error
BUILD FAILED (total time: 0 seconds)

The problem lies with — new “Microwave” —
only the word Microwave has caused this output (I believe).

Does this have to do with the import java.util.Scanner; not being in and working properly, or is it something else, and if it is, could someone please guide me in the right direction?

Thank you so very much for all help!!

This is from the initial file:
public class Microwave
{
private double time;
private int tempLevel;
private boolean isOn;
private boolean turnLightOn;

public Microwave(double Time, int Temp)
{

time = 0.0;
tempLevel = 0;
isOn = true;
turnLightOn = false;
Then, I have what is on the Test file:
import java.util.Scanner;

public class MicrowaveTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

Microwave myMicrowave = new Microwave (7.0, 9, “off”, “off” );

Everything else is working properly; or at least no other errors are popping up.

Hope this is what you needed
thanks!
I have added the perimeters KegC, but I am still having the same problem; Microwave still has the red squigly line under it :( I have created new projects several times and still the same problem.

Microwave myMicrowave = new Microwave (“5:22″, “3″, “off”, “off”);
Thanks Tizio, KegC, and Active! You all lead me in the right direction; however, I was not able to get anything to work except just not including anything in the ‘new’ constructor:
public Microwave () – and on the test class:
Microwave myMicrowave = new Microwave ();

This is how I was able to complete this assignment and where the problem was. Three of you were right about what I should pay attention to and it is hard to decide on which to choose for best answer :(, but will decide on one anyway. As for Blackcome, you should not post to anything you have no clue about. Sorry, but stay away from coding bud!

As for the others, I have lots more to do, so subscribe or watch for more from me :)

Thanks again!

Best answer:

hmm can’t see any problem, post more code…

java.lang is always all imported, so you can write simply String (this should not be an issue anyway)

~~
you call the constructor this way:
new Microwave (7.0, 9, “off”, “off” );

but i can only see one kind of costructor, no overloading:

Microwave(double Time, int Temp)

so the “java” searches for a constructor that fits the “fingerprint” of the arguments double, int, String, String; but can’t find it…

at least one constructor arguments must be the same kind of the one of the caller,

public Microwave(double Time, int Temp, String A, String B)

but why do you use String when all you want is a on/off feature? use boolean instead!

at last you added still another way of calling
new Microwave (“5:22″, “3″, “off”, “off”);
and still there is no “prototype” matching (three strings)
fix this

Tags: , ,

Under Forum

3 Comments for Java – constructor problems?

  • 1. Blackcompe  |  October 5th, 2007 at 11:36 am

    public Microwave(double Time, int Temp)

    Microwave myMicrowave = new Microwave (7.0, 9, “off”, “off” );

    ??

  • 2. KegC  |  October 5th, 2007 at 12:11 pm

    Where you’re creating the Microwave object, you’re passing 4 parameters.

    new Microwave (7.0, 9, “off”, “off” );

    But the constructor you defined only has two parameters.

    public Microwave(double Time, int Temp)

    So it’s just moaning that it can’t find a constructor with 4 parameters.

  • 3. Active  |  October 5th, 2007 at 12:33 pm

    I have analyzed your code , seems a little problem in it.

    CLASS MicroWave:

    public class Microwave
    {
    private double time;
    private int tempLevel;
    private boolean isOn;
    private boolean turnLightOn;

    public Microwave(double Time, int Temp)

    “You are creating a two argument constructor and you are giving 4 arguments while creating an object.”

    {
    time = 0.0;
    tempLevel = 0;
    isOn = true;
    turnLightOn = false;
    }

    public class MicrowaveTest
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);

    //TRY THIS CODE ONLY
    Microwave myMicrowave = new Microwave (7.0, 9);
    }
    }
    };

Leave a Comment for Java – constructor problems?

You must be logged in to post a comment.

Trackback this post  |  Subscribe to the comments via RSS Feed


Categories