View Full Version : got my compiler running, now i need help
Uber Fr0g
02-19-2006, 12:58 AM
I got it to work properly, but now ive come across a problem in the book that is stumping me since im a newb i dont really know where to begin.
Its problem EX. 4.18 on page 173-174 (the credit limit problem) in the deitel 6th edition how to program Java book. If anyone has the book.
Problem: For each customer this info is available:
account number
balance at beginning of month
total of all items charged by customer this month
total of all credits applied to the customer account
allowed credit limit
The program should input all these facts as integers,calculate the new balance (= beginning balance + charges - credits) display new balance determine if it exceeds the credit limit of the customer, print credit limit exceeded if the customer limit is exceeded.
I have no idea where to begin....i have no knowledge of the workings of a credit card...especially the "credits applied" i just use debit cards lol. I wrote down a little algorithm to create a sort of outline. As for coding i have no idea where to start or even how. I hate to sound like such a newb....but i am.
Ender
02-19-2006, 01:16 AM
Here's a possible design (purposefully non-detailed):
Create a Bank class and a ClientAccount class. The Bank class will be a Simple Factory design pattern. The Simple Factory design pattern basically takes some input information from a requestor and returns something to the requestor based on that input information. In this case, the Bank class would hold a database of ClientAccounts, and it would provide methods for returning a ClientAccount based on input information. The requestor object would then talk to the ClientAccount, based on what the end-user does through the UI.
Okay, I'll go into some more detail.
The ClientAccount class should hold information on each account's information, such as the account number, the balance, etc. The Bank should have functionality for creating new accounts and accessing each account. It should have a database for all accounts as well. Perhaps you should make a binary search tree, if you want to operate efficiently. If you do your search tree based on account number, you will have to sort your account numbers so as to achieve a balanced search tree, or else you will achieve a pointless linear tree. If you do it by the registrant's name (using the character mappings, i.e. ASCII, UNICODE, etc.) then you will, in theory, achieve a somewhat balanced search tree.
If your real problem is that you don't understand credit cards, google it. If you don't know enough Java to get started, then ask specific questions about the language. You said you just got your compiler working, but you're on page 173, so I'm confused at how familiar you are with the language.
Spookster
02-19-2006, 01:27 AM
Well I would begin by reading up and understanding object oriented programming. This problem is a perfect example to apply this technique. In this case the object you want to create is a bank account.
http://java.sun.com/docs/books/tutorial/java/concepts/object.html
You've been given a list of things you need to keep track of.
accountNumber
beginningBalance
totalCharges
totalCredits
maxCredit
To explain what credits and charges are. A credit card is a type of loan. The bank loans you money up to a certain limit. A credit card starts out with a limit or is also called a line of credit. Let's say that limit is $5000. This means you can use it to buy up to $5000 worth of stuff. A charge is the amount deducted from that limit. Your balance is your limit minus the amount of charges. A credit is an amount added to your balance. So when you buy something it is a charge. So let's say you bought a computer and used your credit card. That computer costs $2000. Subtract that from you limit which is $5000 and now you have a balance of $3000. You have total charges of $2000. Let's say you payed your credit card bill in the amount of $1000. That $1000 is a credit. You add the credit to your balance and it gives you a new balance of $4000.
To start this problem create an outline of your account class which will be the object you want to create. Within that class figure out what data members you will need. This will be the data you want to keep track of. Next figure out what methods you will need to create to process the data like figuring out the balance or processing charges and credits, etc.
Uber Fr0g
02-19-2006, 07:13 AM
well, most of the programs ive been doing by hand and checking with my friends final progams that he has from last semester. But now ive come across the harder programs and he didnt have to do these. So i downloaded a compiler to code with and im hacking away at them. Thanks for all the info its 1am, so ill probably get going more on this tommorow.
Uber Fr0g
02-19-2006, 07:54 PM
ok, im up to here. This is a cleaned up version of the code i wrote earlier(mine was much longer lol)
public class project1
{
public static void main(String[] args)
{
int beginningBalance = 400; // how much you owe
int totalCharges = 500; // what you spent/charged this month
int totalCredits = 200; // last payment to the credit company
int creditLimit = 500; // max amount you can owe on this account
// compute current balance
int balance = beginningBalance + totalCharges - totalCredits;
System.out.println("balance = " + balance);
if(balance > creditLimit)
{
int excess = balance - creditLimit;
System.out.println("credit limit exceeded by " + excess);
}
}
}
Ive been trying to adjust this to where the user enters the values i specified at the beginning of this post. But, whatever i try seems to not compile or not work. Can i just set the initial values at the beginning to 0 like when programming in C? and thn get the user input. If so ive been trying that different ways and its not working.
Ender
02-19-2006, 10:06 PM
In Java, fields can be declared without definition. Local variables must be defined. Here's some an example code, and it's output:
class Example
{
private int myField; // field
private int precendenceEx = 100;
public static void main(String[] args)
{
int myLocalVar = 1;
System.out.println(myLocalVar);
System.out.println(myField)
int precedenceEx = 99;
System.out.println(precedenceceEx);
System.out.println(this.precedenceEx);
}
}
Output:
0
1
99
100
Local variables are variables inside a method. They can be used by any members inside the method, and are forgotten after the method finishes execution. Fields are variables that are not forgotten. They take different access restrictions. Private means that they can be used only by objects of the class they are in. Protected means that they can be used by objects of the class they are in and objects of all child classes (not parent classes, however). Public means they can be used by all objects.
Local variables must be initialized upon declaration. Fields don't have to be. Fields, such as myField in the example above, are set to default values when not initialized.
Fields can either have class or instance scope. Class scope is when you use the "static" keyword. Instance scope is when you don't use the "static" keyword. Class scope is when a variable is the same for all objects of the class the variable is in; the variable belongs to the class. A static/class variable can be accessed by referencing the class it belongs to. The "static" keyword may throw you off - static/class variables are not constant (that is denoted by "final"). Static variables simply have a static memory address. Instance variables belong to the object alone, and they can be accessed by referencing the object they belong to. The same instance variable in different objects have different memory addresses. Instance variables use dynamic memory, whereas static/class variables use static memory addresses.
Static/class fields are not recommended, as they're not OO. A Java program is supposed to be a bunch of mutually exclusive objects talking to eachother through their interfaces. Static variables makes those objects share some members. However, as ideal theories are not always practical, static variables are used sometimes because they can be useful. Many people say that Java is 95% OOP.
You may also notice that I made my fields private in the above example. This is because of the concept of encapsulation, which states that objects should access eachother's data through eachother's interface (public methods) instead of through public fields (global variables). If you're familiar with global variables, you may have heard that they're "evil." This is because if an object accesses another object's data directly, then changes to the latter object's data would affect all objects that access that data. Interfaces, however, are more set-in-stone, and changes to an object's interface occur much less frequently and are easier to adjust to in the rest of the program. With encapsulation applied, changes to an object's data will stay at home and not ripple throughout the program.
Ender
02-19-2006, 11:31 PM
I'm not sure if that answered your question in my last post o.o, I kind of rambled on, so I'll give a quick code example of a UI.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class QuickUI
{
public static void main(String[] args)
{
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input value: ");
int input = 0;
try {
input = Integer.parseInt(cin.readLine());
} catch (IOException e) {
System.out.println(e.toString());
e.printStackTrace();
}
System.out.println("You just inputted the number " + input);
}
}
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.