Results 1 to 7 of 7
- 10-13-2011, 04:28 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
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); } }
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"); } } } }
- 10-13-2011, 06:21 PM #2
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.
- 10-13-2011, 06:39 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
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!
- 10-13-2011, 06:44 PM #4
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); } }
Regards Serb.
- 10-13-2011, 07:20 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Re: Please help fix error for two programs!
Awesome! It works!!! Thanks a ton!!!!!!!!! :)
- 10-13-2011, 11:57 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 66
- Rep Power
- 0
Re: Please help fix error for two programs!
Can anyone help me out with the second program?
- 10-14-2011, 12:59 AM #7
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
-
robust programs
By dntcheatme in forum New To JavaReplies: 6Last Post: 05-03-2011, 09:54 PM -
Writing a programs
By smray7 in forum New To JavaReplies: 7Last Post: 04-28-2011, 08:49 AM -
Cant run my programs anymore
By Glenn1990 in forum New To JavaReplies: 2Last Post: 02-25-2011, 09:49 PM -
how can i package my programs for installation
By buston01 in forum Advanced JavaReplies: 1Last Post: 01-25-2010, 01:41 PM -
I need a simple programs
By mikau in forum New To JavaReplies: 2Last Post: 02-11-2008, 04:37 AM
Bookmarks