?
Solved

[noob] [java] what is wrong with this code?

Posted on 2007-10-13
5
Medium Priority
?
216 Views
Last Modified: 2013-11-23
public class Microwaveoven
{
      private int maxpower;
      private int percent;
      private int minute;
      private int sec;
      
      public void announce()
      {
            System.out.println ( " Hello world! this is my java code done from remembering how the other thing was done " );
      }

      
      public void Microwaveoven ( int power )
      {
            maxpower = power;
            
      }
      
      public void set ( int minutes, int secs)
      {
            minute = minutes;
            sec = secs;
      }
      
      public void power ( int p )
      {
            percent = p;
      }
      
      public void start ()
      {
            System.out.println ( "It will cook at " + percent + " percent power for " + minute + "minute and " + sec+ "seconds" );
      }
      
      
      
      
      
      public static void main ( String[] args) throws Exception
      {
            Microwaveoven oven1 = new Microwaveoven ( 10 );
            oven1.set(5, 10 );
            oven1.power(50);
            oven1.start();
            
            
      }
      
}
0
Comment
Question by:Troudeloup
5 Comments
 
LVL 11

Expert Comment

by:Jeffrey Dake
ID: 20071567
What issues are you having?  Please explain what is happening that is wrong.
0
 
LVL 17

Expert Comment

by:gops1
ID: 20071639
remove void from the constructor
0
 
LVL 3

Expert Comment

by:CPlusJavaCSharp
ID: 20074765
Actually you need to remove public and void from the constructor....
0
 
LVL 14

Expert Comment

by:CPColin
ID: 20079871
The public keyword can stay (if you do want it to be public), but void definitely has to go.
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 2000 total points
ID: 20087664
A slightly cleaned-up version of the same program, which fixes the constructor (as gops1 indicated), uses Java naming conventions for methods, adds a placeholder for error handling, and adds spaces to the printout. You may also consider adding getter methods for your instance variables.

Michael

public class MicrowaveOven
{
      private int maxpower;

      private int percent;

      private int minute;

      private int sec;

      public void announce()
      {
            System.out
                        .println(" Hello world! this is my java code done from remembering how the other thing was done ");
      }

      public MicrowaveOven(int power)
      {
            maxpower = power;

      }

      public void setTime(int minutes, int secs)
      {
            minute = minutes;
            sec = secs;
      }

      public void setPower(int p)
      {
            if (p > maxpower)
            {
                  // reject the value
            }
            else
            {
                  percent = p;
            }
      }

      public void start()
      {
            System.out.println("It will cook at " + percent + " percent power for "
                        + minute + " minute and " + sec + " seconds");
      }

      public static void main(String[] args) throws Exception
      {
            MicrowaveOven oven1 = new MicrowaveOven(10);
            oven1.setTime(5, 10);
            oven1.setPower(50);
            oven1.start();
      }
}
 
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question