473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

I need some help guys..

2
I am currently studying java and i came across a question.

The following program has several defects (errors) that will generate compile-time errors, logical errors (unintended behaviour) or run-time errors (forced termination). Identify and correct 5 such defects by rewriting this program and identifying and correcting the errors. You may identify an error (and the fix for it) using a comment directly above the line that was corrected.
import java.io.*;
/* Read a sequence of positive integers from standard input
** one integer per line, terminating with 0, and print the
** minimum, maximum and sum of the integers to standard output
*/
public class Main {
private BufferedReader in;
public static void main(String[] args) throws IOException {
Main app = new Main();
app.run();
}

public Main() {
in = new BufferedReader(new StreamInputReader(System.in));
}
public void run() {
int min = 0;
int max = 0;
int sum = 0;
number = Integer.parseInt(in.readLine());
while(number > 0) {
if (number > max) {
max = number;
}
if (number < min) {
min = number;
}
sum = sum + number;
}
System.out.println(“Minimum: “ + min);
System.out.println(“Maximum: “ + max);
System.out.println(“Sum: “ + sum);
}
}

I only seem to find one error in this.Can you guys help me out with this?
May 16 '06 #1
3 1611
Banfa
9,065 Expert Mod 8TB
What is the error that you see?
May 16 '06 #2
nish
2
instead of
private BufferedReader in;
public static void main(String[] args) throws IOException {
Main app = new Main();
app.run();

I thought it could go like

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(
new StreamInputReader(System.in));
int min = 0;
int max = 0;
int sum = 0;
May 17 '06 #3
Banfa
9,065 Expert Mod 8TB
OK well that may be, I am not a Java programmer but a C programmer so that is behond what I know but it that correction fixes a compiler error or a run time error then it's good, I don't think it fixes a logic error.

However the syntax of the language is close enough for me to see at least 2 logic errors.

In the function public void run() consider

1. What will happen to the variable min if none of the inputs are < 0 and none of them will be as a negitive number is used as the end condition of the loop?

2. What will happen in the while loop if the first input is < 0?

3. Is in.readLine() garuntteed to return digits only, what happens if "the" is returned bu in.readLine()?
May 17 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Snake | last post by:
Hi guys, I have question about classes. when u create class called Test. and you define variable Test c; so does this act like( a variable c of type Test pointing to an abject )? The thing that...
13
by: Joe Black | last post by:
Just to inform you guys that i have only like 2 weeks that i took my first classes in c++, and my proffesor now is asking me to solve this problem: /// Using a function create a Win32 Console...
8
by: Tim Geiges | last post by:
Since I am being challenged with learning c# I figured I could pass some of the pain on to you guys :-) I have another question(this one is important for me to fix before I can get my app to Beta)...
3
by: Lucky | last post by:
hi guys, i'm practising regular expression. i've got one string and i want it to split in groups. i was trying to make one regular expression but i didn't successed. please help me guys. i'm...
0
by: Michael L | last post by:
Hi Guys(I apologize for the lengty post - Im trying to explain it as best i can) I've been cracking my head on this one for the past 24+ hours and i have tried creating the function in ten...
28
by: Gaby | last post by:
Hi guys, I have this ASP code that is a little long to post so but you can download it off of http://whatthedeuce.net/Form.asp I cant find out what my error is. I know it is not in my Access...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
6
by: Scotty Montague | last post by:
hi guys! i own 2005 ford mustang convertible and i want to make it go faster. my friend barry's told me that i can reprogram computer in my car and boost the horses. i assume computer program is...
1
by: skygenn | last post by:
hi guys im new at c++ program and im 1st year taking up I.T our prof in c++ gave us an assignments but she didnt even explain us how to begin doing it? honestly i have no idea how to begin it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.