Java question from programming novice

cr0ssfire

Senior member
Sep 10, 2005
379
0
0
This is pretty embarrassing to admit, but I'm already lost in my Principles of CS II class. The book we're using Data Structures and Algorithm Analysis in Java, 2nd Edition by Mark Allen Weiss. I don't know if any of you are familiar with it, but it's written in such a way that nobody in my class understands what it's even trying to explain a good bit of the time, and even the professor himself doesn't understand some of the material in chapter one. Why we're still using it, then, I have no idea. But I've got some homework that's due tomorrow, and I'm stuck on one particular problem:

"C allows statements of the form

#include filename

which reads filename and inserts its contents in place of the include statement. Include statements may be nested; in other words, the file filename may itself contain an include statement, but, obviously, a file can't include itself in any chain. Write a program that reads in a file and outputs the file as modified by the include statements."

I don't even know where to begin on this. We didn't cover anything of this sort in Principles of CS I, and as seems to be the norm, Mr. Weiss conveniently neglects to talk about it or even mention it in passing. Can I get a little help here?
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
...I'm not sure exactly how to help you. The way to approach the problem seems quite obvious. Basically, you're writing a preprocessor that handles #include statements. You do know how to do file-based input and output, right?

 

beyonddc

Senior member
May 17, 2001
910
0
76
Like Matthias99 said, it sounds pretty obvious.
I found it strange that the book is teaching you Java and asking you to do a C/C++ assignment.
 

cr0ssfire

Senior member
Sep 10, 2005
379
0
0
Originally posted by: beyonddc
Like Matthias99 said, it sounds pretty obvious.
I found it strange that the book is teaching you Java and asking you to do a C/C++ assignment.

I thought that it was asking me to write a C/C++ program as well, but I e-mailed the professor to ask him why I was being expected to program in a language that isn't offered at the college and he says that I'm to do this in Java. I took a little C++ in high school, but I don't remember any of it. For that matter, I'm just getting started with programming in Java, and I haven't really done any file-based input and output in Java outside of the cursory day or two last we spent on it last semester during class.
 

Matthias99

Diamond Member
Oct 7, 2003
8,808
0
0
Originally posted by: cr0ssfire
Originally posted by: beyonddc
Like Matthias99 said, it sounds pretty obvious.
I found it strange that the book is teaching you Java and asking you to do a C/C++ assignment.

I thought that it was asking me to write a C/C++ program as well, but I e-mailed the professor to ask him why I was being expected to program in a language that isn't offered at the college and he says that I'm to do this in Java. I took a little C++ in high school, but I don't remember any of it. For that matter, I'm just getting started with programming in Java, and I haven't really done any file-based input and output in Java outside of the cursory day or two last we spent on it last semester during class.

No, it is clearly asking you to implement this (C/C++ preprocessor) functionality in Java. I don't know how it could be much clearer (especially since it's in a Java book...).

It's been a while since I've had to do any Java, so I can't give you much in the way of syntactical advice without going to do research.

But basically you want to read in the input file one line at a time. If the line doesn't start with #include, just output it unchanged. If it does start with #include, use the rest of the line as a filename and recursively do the same thing to that file (inserting it into the output file/stream at that point).

In theory, when you recurse you should also keep track of which files you have recursed through. If you open the same file again (which would end up creating an infinite loop) you should bail out with an error.
 

itachi

Senior member
Aug 17, 2004
390
0
0
oh god i feel sorry for you.. weiss' books on algorithms are quite possibly the worst books to learn from.

if i were you, i'd seriously consider buying a different book.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |