• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

General Questions...

few questions from a past paper im studying.. i do not mind at all if answers are not given as a tip in the right direction would be much appreciated.

1) rewrite power() method using iteration instead of recursion
public int power(int base, int exp)
{
   if (exp<=1)
       return base;
   else
return (base * power(base, exp -1));
}
===================================
2) A few disadvantages of inheritance

3) A few advantages of polymorphism
==========================================================
3b) Describe what objects are instantiated in the main program and what relationship they have to the class(es).

class Messages2 {
public static void main (String[] args) {
Thought territory = new Thought ();
Advice cliche = new Advice();
territory.message();
cliche.message();
territory = cliche;
territory.message();
} //method main
} //class Messages2
class Thought {
public void message() {
System.out.print ("Some people get lost in thought
because it’s unfamiliar territory.");
System.out.println();
} //method message
} // class Thought
class Advice extends Thought {
public void message() {
System.out.print ("Avoid clichés like the plague.");
System.out.println();
} //method message
} //class Advice

==========================================================

4) What happpend when an exception is thrown but no method has explicitly been written to handle it?
0
pete420
Asked:
pete420
  • 6
  • 3
1 Solution
 
bloodredsunCommented:
As you mention, this is for a graded assignment so we can only give general information not codes.

1./ You need to use either for() or while() to do this

2. and 3. you can find from the web. 3b requires some knowledge of Object Orientated programming.

4./ Write a program and see what happens
0
 
bloodredsunCommented:
and in 3b, the comments tell you most of what you need to know
0
 
pete420Author Commented:
ok thats fair enugh,

since u cant really help me much on them (not your fault, gota keep to the rules i guess)
maybe u can help me on this problem,. it is another question but i have made a more than fair attempt at it and its not working properly. so ir eally just want help t debug it.

"Write a Java method called doIt and exception handling code to generate the
result called quotient of one number called numerator divided by another
number called denominator passed as parameters. The doIt method should
return the quotient as long as the denominator is not equal to zero. If the
denominator is zero an exception should be generated and handled to print the
following message – “Dividing by Zero” to the standard screen output stream."

my attempt:
=========
public class division
{

            public static double doIt (int numerator, int denominator) throws ArithmeticException
            {
                  double quotient = 0;
                  try
                  {
                        quotient = (numerator / denominator);
                  }
                  catch(ArithmeticException arithmeticException)
                  {
                        System.out.println("Dividing by Zero ");
                  }
                  return quotient;
            }
      
      


      public static void main (String[] args)
      {
            System.out.println("answer: " +doIt(3,10));
      }
}
============

problems with it are when u do this instead of returning 0.3 it returns 0.0 also, if u have

doIt(3,0), instead of just printing the cant divide by zero it also posts 0.0 as well.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bloodredsunCommented:
You need to look at the fact that Java math is done in ints by default
0
 
pete420Author Commented:
when i change denom and numerator to floats it works however when they are changed to floats the catch block does not work, instead it outputs the word "infinity"
why is the catch block not catching anymore?

pete
0
 
bloodredsunCommented:
Do this:
quotient =  (double)numerator / (double)denominator;

and you will need to throw your own exception if they are zero:
throw new ArithmeticException() ;
0
 
pete420Author Commented:
the first bit worked fine but the 2nd bit didnt work properly i dont think

public class division
{

            public static double doIt (int numerator, int denominator) throws ArithmeticException
            {
                  double quotient = 0;
                  try
                  {
                        quotient = ((double)numerator / (double)denominator);
                        throw new ArithmeticException() ;
                  }
                  catch(ArithmeticException arithmeticException)
                  {
                        System.out.println("Dividing by Zero ");
                  }
                  return quotient;
            }
      
      


      public static void main (String[] args)
      {
            System.out.println("answer: " +doIt(3,0));
      }
}

thats my code now, and my output says "cant fivide by zero" but then it adds in this Answer: infinity. i dont want it to ouput this, just be caught by the catch block..

thanks
0
 
bloodredsunCommented:
Here's an example for you to look at, it's not perfect but it does the job:

public class Quotient {

      public static double doIt(int numerator, int denominator) {
            double quotient = 0;
            try {
                  if (numerator == 0 || denominator == 0 ){
                        throw new ArithmeticException() ;
                  }
                  quotient =  (double)numerator / (double)denominator;
                  return quotient;
            } catch (ArithmeticException arithmeticException) {
                  System.out.println("Dividing by Zero ");
            }
            return -1 ; //default error double
      }

      public static void main(String[] args) {
            double result = doIt(56, 6) ;
            if (result != -1){
                  System.out.println("answer: " + result );
            }
      }
}

You need to move the exception handling code to the correct place adn you're done
0
 
bloodredsunCommented:
thanks....
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now