Results 1 to 1 of 1
- 07-30-2012, 04:11 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
Program using Input file, While loops, decisions (if/else), control break,accumulator
Hello,
I am new here, and to Java (in fact, I'm taking a programming logic course, not even a Java course, so anything I know, I have learned from A Beginner's Guide to Programming Logic and Design (Farrell) and Java Programs to Accompany Programming Logic and Design (Smith). In addition, please feel free to suggest previous topics that answer my questions, links to pages/free resources I can read to learn about beginning Java, etc. I admit that I am coding without really understanding what I'm doing; I look through my books and on the Internet for possible solutions or tips. I am trying to understand, but, well, I need help.
What the program should do:
It should take data (5 lines of integers, with each line having 5 different numbers, white space delimited) from an input file. The 5 numbers on each line represent the from area code, from phone number, to area code, to phone number, and number of minutes the call lasted (in that order). The 5th and final line is just 0s, for the sentinel value.
As long as it doesn't reach a sentinel value of 0 for the "from area code," the program loops, calculating the cost of the call.
Decision portion: the program has to make decisions, depending on the area codes and length of the call, when calculating the cost of a call.
Control break logic portion: If the "to area code" changes, the program should calculate the total cost of the calls to that particular area code (accumulator portion).
The output should look like:
Call Log and Billing Report
===========================
Call from 123 4567890 to 321 7777700 for 25 minutes costs $4.0
Call from 123 4567890 to 321 5555555 for 15 minutes costs $2.0
Total costs for calls to 321 is $6.0
Call from 123 4567890 to 800 2222222 for 10 minutes costs $0.0
Call from 123 4567890 to 800 3333333 for 10 minutes costs $0.0
Total costs for calls to 800 is $0.0
I don't think it matters if the output to the screen happens as the program runs or if it comes only after the program has finished (reached the sentinel value). I would think the former makes more sense, but I'm not sure if one is easier to code.
My questions:
Are lines 39 and 40 necessary?
Line 42: should it be an "if" or a "while"?
There is something wrong with lines 42 and 102 (error says "incompatible types") and 52 and 96 ("int cannot be dereferenced"). Should 0 instead be "null"?
I'm sure there are other things wrong with this code...I appreciate any help.
I could not attach my .txt file, so the contents are (white space delimited):
123 4567890 321 7777777 25
123 4567890 321 5555555 15
123 4567890 800 2222222 10
123 4567890 800 3333333 10
0 0 0 0 0
Java Code:/* Class : STB2 * Author: MS * Design: Java program to calculate and report the cost of input call records, with total cost of calls to same area code listed when area code changes */ import java.io.*; public class STB2 { public static void main(String[] args) throws Exception { final int FREE_AREA = 800; // named constants final int TIER_MINUTES = 20; final double CONNECTION_FEE = .50; final double FREE_CALL = .0; final double LOW_RATE = .10; final double HIGH_RATE = .15; int fromArea; // input variables int fromPhone; int toArea; int toPhone; int callLength; double callCost; // calculated cost double callTotal; // total for same area codes calls // Work done in the getReady() method FileReader fr = new FileReader("STT.txt"); BufferedReader br = new BufferedReader(fr); String str = ""; callTotal = 0; int oldToArea; boolean done; done = false; oldToArea = toArea; fromArea = Integer.parseInt(stringFromArea); toArea = Integer.parseInt(stringToArea); while((fromArea = br.readLine()) != 0) { done = false; oldToArea = toArea; } else done = true; while(done == false) { // This is the work done in the produceReport() method if(toArea.compareTo(oldToArea) != 0) { // logic to determine applicable call rates { if(toArea == FREE_AREA) // call to free area code { callCost = FREE_CALL; } else { if(toArea == fromArea) // call to same area code { if(callLength <= TIER_MINUTES) { callCost = FREE_CALL; } else { callCost = LOW_RATE * (callLength - TIER_MINUTES); } } else // calls to external area { if(callLength <= TIER_MINUTES) { callCost = CONNECTION_FEE + LOW_RATE * callLength; } else { callCost = CONNECTION_FEE + HIGH_RATE * TIER_MINUTES + LOW_RATE * (callLength - TIER_MINUTES); } } } } { System.out.println("Call Log and Billing Report"); System.out.println("==========================="); System.out.print("Call from " + fromArea + " " + fromPhone); System.out.print(" to " + toArea + " " + toPhone + " for " + callLength); System.out.println(" minutes costs $" + callCost); } // This is the work done in the controlBreak() method if(toArea.compareTo(oldToArea) != 0) { System.out.println("\t\t\tTotal for all calls into " + oldToArea + " is " + callCost); callTotal = 0; oldToArea = toArea; } if((fromArea = br.readLine()) != 0) { done = false; } else done = true; } // This is the work done in the finishUp() method System.out.println("\t\t\tTotal for all calls into " + oldToArea + " is " + callCost); br.close(); System.exit(0); } } }
Similar Threads
-
Need Help with Homework Lab Assignment on Loops and File Input/Output
By Terminus_Est in forum New To JavaReplies: 7Last Post: 02-29-2012, 01:15 PM -
help me in this program please (break and continue statement s)
By funkygarzon in forum New To JavaReplies: 10Last Post: 01-07-2011, 10:28 AM -
Control if it is a input
By Torgero in forum New To JavaReplies: 2Last Post: 03-11-2009, 12:14 AM -
[SOLVED] getting program's math right from file input
By gotenks05 in forum New To JavaReplies: 4Last Post: 02-20-2009, 01:26 AM -
How to remove Control Characters from an input file?
By renjan in forum Advanced JavaReplies: 0Last Post: 08-01-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks