 |
 |
Java Compiling problem
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Oct 2003
Status:
Offline
|
|
Hi,
I'm doing this assignment, the question is below and below that is what I've done. However, it doesn't compile and run and the more I try to fix it the more confusing it gets, please help!
The question:
The objectives of this question are:
1. To use the Java exception handling facility in a program
2. To define and use an exception class
3. To write program code that will throw user-defined exceptions
In this question, you are asked to write a program to accept a series of command line arguments. Assume that the arguments will give a series of positive integers which we will identify as n1, n2, n3, n4, . . . The program should check whether the integers n2, n3, n4 and so on are factors of n1. You may assume that there will be at least two positive integers n1 and n2.
Here are what you have to do to achieve this:
1. Create an exception class FactorException as a subclass of Exception. An object of this class is to be created when given two integers, x and y, y is not a factor of x. The constructor of this class should show the message "Exception thrown: y is not a factor of x " when activated to create the exception object.
2. Write a method factor( ) that will accept two integers as arguments. The method should check whether the second argument is a factor of the first and throw an exception if it is not. Otherwise, the method should return true. Your method should use both the throw and throws keywords.
3. Write a main( ) method that will accept the command line arguments as described above. Use the method factor( ) to check whether each of the integers, n2, n3, n4, . . . are factors of n1. The following is an example to show what is required: Assume that the command line arguments are 24 6 2 5 4 The program should show the following output: Student id: xxxx 6 is a factor of 24 2 is a factor of 24 Exception thrown: 5 is not a factor of 24 4 is a factor of 24 You should replace xxxx with your actual student id.
Submit the following:
(a) Your implementation of the FactorException class [4 marks]
(b) Your implementation of the method factor( ) [6 marks]
(c) Your implementation of the method main( ) [10 marks]
(d) A screenshot (printscreen) showing the output from one run of your program. [2 marks]
(e) Identify from your programs where the following takes place: [2 marks]
The exception class is defined
The exception class is used (state the actual Java statement)
(f) Make modifications to your methods to investigate which of the following is true: [6 marks] If a finally block is present, the statements it contains are always executed when controls leaves the try block. If the finally block is present, the catch clauses are optional. Include a copy of the code that you have used in your investigation. You only need to give a brief description of your investigation.
What I've done:
import javax.swing.*;
import java.awt.*;
class FactorException extends JFrame {
int num = 0, divisor = 0, result = 0;
boolean ThereIsError = true;
public static void main(String[] args) {
FactorException frame = new FactorException();
frame.doMathematics();
System.exit(0);
}
private void doMathematics() {
do {
num = getNum("Enter number: ");
} while (ThereIsError);
do {
divisor = getNum("Enter the divisor ");
} while (ThereIsError);
try {
result = num / divisor;
}
catch ( ArithmeticException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this, "Divisor cannot be zero!!! \n"
+ e.toString());
result = 0;
ThereIsError = true;
}
if (!ThereIsError)
JOptionPane.showMessageDialog(this, num + " / " + divisor +
" = " + result);
System.exit(0);
}
private int getNum(String message) {
String str = "";
int number = 0;
ThereIsError = false;
try {
str = JOptionPane.showInputDialog(this, message);
number = Integer.parseInt(str);
}
catch (NumberFormatException e) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this, "Error: " + e.toString());
ThereIsError = true;
}
return number;
}
public static void factor(int n){
int d;
if (n > 1){
for (d = 2; n % d > 0; d++);
System.out.print(d + " ");
factor(n / d);
}
}
}
What am I doing wrong?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status:
Offline
|
|
Originally posted by 1zero:
However, it doesn't compile
How about giving us the compiler output for a start...
|
|
signatures are a waste of bandwidth
especially ones with political tripe in them.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status:
Offline
|
|
And why are you extending JFrame?
You assignment specifically says to extend "Exception".
|
|
signatures are a waste of bandwidth
especially ones with political tripe in them.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status:
Offline
|
|
Ok, after looking at what you have, it's apparent that you need to go to office hours or meet with the T/A. You have some fundamental issues you are missing.
Yeah, we could do your homework for you, but you're only cheating yourself. See your prof--pronto.
|
|
signatures are a waste of bandwidth
especially ones with political tripe in them.
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Oct 2003
Status:
Offline
|
|
Thanks, I did think I needed serious help.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|