Results 1 to 7 of 7
  1. #1
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Please help fix error for two programs!

    Hey guys, my teacher gave me two programs and purposely gave the wrong code to see if I can fix it.. However, I don't understand what's causing the errors and how I can fix it.. I have two programs and here they are:

    1)

    Java Code:
    public class cw106a
    {
    	public static void main(String[] args)
    	{
    		new worker().doOverload();
    	}
    }
    class worker
    {
    	public void doOverload()
    	{
    		double w = 3.2;
    		double x = 4.2;
    		int y = square(w);
    		double z = square(x);
    	}
    
    
    	public int square(double y)
    	{
    		return(int)(y*y);
    	}
    
    	public double square(double y)
    	{
    		return (y*y);
    	}
    }
    2)

    Java Code:
    public class cw106b
    {
    	public static void main(String[] args)
    	{
    		new worker.doIt();
    	}
    
    	public class worker
    	{
    		void doIt()
    		{
    			Base myVar = new A();
    			myVar.test();
    		}
    
    		public class Base{    };
    
    		public class A extends Base
    		{
    			public void test()
    			{
    				System.out.print("A");
    			}
    		}
    	}
    }
    I have attached files showing the errors.. the first one is the first program and the second one is for the second program.. any help will be much appreciated! Thank you!
    Attached Thumbnails Attached Thumbnails Please help fix error for two programs!-cw106a.jpg   Please help fix error for two programs!-cw106b.jpg  

  2. #2
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: Please help fix error for two programs!

    In the first program you have duplicate methods within the same class. So change the name of one of the square methods. For instance you could change the square method that returns a double value to "square1", you will also have to alter the what the double variable z is equal to...

    Use System.out.println for both y and z to ensure that they are being passed the correct values.

    Hope this helps,

    Regards Serb.

  3. #3
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Please help fix error for two programs!

    Thanks for replying!

    1) What alterations should I make to the variable z? Should I change it to an int?
    2) Where should the system.out.println statements for y and z go? I tried to put it in the class with the main method but it gave me an error. It also gave me an error when I put it in the second class!

  4. #4
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: Please help fix error for two programs!

    Java Code:
    public class cw106a
    {
        public static void main(String[] args)
        {
            new worker().doOverload();
        }
    }
    class worker
    {
        public void doOverload()
        {
            double w = 3.2;
            double x = 4.2;
            int y = square(w);
            double z = square(x);//if the method that returns a double is amended to "square1" double z will need to be made equal to "square1(x);"
            //place the println's here because variables y and z are local variables
        }
     
     
        public int square(double y)
        {
            return(int)(y*y);
        }
     
        public double square(double y)
        {
            return (y*y);
        }
    }
    Hope this makes sense,

    Regards Serb.

  5. #5
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Please help fix error for two programs!

    Awesome! It works!!! Thanks a ton!!!!!!!!! :)

  6. #6
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Please help fix error for two programs!

    Can anyone help me out with the second program?

  7. #7
    Join Date
    Jul 2011
    Posts
    30
    Rep Power
    0

    Default Re: Please help fix error for two programs!

    Your teacher set this to see what you can do. So I'll only give you a hint for the second assignment. Note in this piece of code there are nested classes. To find out more about nested classes read the following: Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    The rest is up to you :)

    Regards Serb.

Similar Threads

  1. robust programs
    By dntcheatme in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 08:54 PM
  2. Writing a programs
    By smray7 in forum New To Java
    Replies: 7
    Last Post: 04-28-2011, 07:49 AM
  3. Cant run my programs anymore
    By Glenn1990 in forum New To Java
    Replies: 2
    Last Post: 02-25-2011, 08:49 PM
  4. how can i package my programs for installation
    By buston01 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2010, 12:41 PM
  5. I need a simple programs
    By mikau in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •