Question:
Help writing a java program?
Pamela
2013-03-25 17:05:44 UTC
I need to write 2 different java programs using while loop and for loop. I have to compute how much the balance in a bank account will be in 5 years if $25.00 is deposited every month and the interest rate is 1/4 percent per month compounded monthly
Five answers:
Barolb
2013-03-26 06:30:42 UTC
Well, you certainly are right about me, Mathew. I'm just a computer science student, but i like hanging around here. Having spent a few days of my life doing Java already (no joke), i've become alright at it, although not as good as some people out there.







Now then, back to the question. Just so you know, i won't write the program for you. You're asking for help on writing it so i'll guide you through it, but at the end you will have to write the programs. First, lets start off with the easier one, the for loop program.





In a typical for loop you have the following:



for (expression1; expression2; expression3)

{



}



And in this format you would generally see someone declare a variable in expression 1. In the second expression you would see a condition, and in the third you would see a result. The result will occur if the second expression is valid. Until the expression is nolonger valid, it will continue looping.



Say for example:



for (int i = 0; i < 5; i++)



A variable int i is declared and assigned the value 0. i (which is basically 0) is less than 5. The condition is then checked. Since i = 0, and 0 is less than 5, the expression is true, and the code below it is ran through. Following this, before the loop repeats its self i increments. So, i is nolonger 0, but becomes 1 at the start of the next iteration of the loop, and it will continue doing this until i is nolonger less than 5. This will occur when i = 5 as i will then nolonger be less than 5, but it is equal to 5. If you want the value to go until it's nolonger less than or equal to 5, then you would use i <= 5.



Keep in mind that the third expression can be used for prefix (++i or --i) and postfix (i++ or i--), or for any sort of incrementing or decrementing like (i+=2, also read i = i + 2 or i-=2, also read i = i - 2).





Now, for the for loop portion, think about what a year is made up of. A year is made up of days, weeks and months, and every month $25 is deposited (how many months are in 5 years?). The interest rate is compounded monthly (To be honest, i'm a little fuzzy on compound interest rate), and you would be adding the interest rate to the total every month. In order to do that, you would multiply the total by the interest rate to obtain how much money a person gained from interest, and then add it to the total. You don't want your values mixed up so what would you do?





So, in your program you would require a total, adding 25.00 to the total every month, and adding the interest rate after every month as well. The interest rate is a fixed amount (1/4 = 0.25), but the total is not at a fixed amount.





If you can complete the program using a for loop, then you won't have as much trouble with a while loop. However, to help a bit with that, i'll make a note or two below.



With while loops, all you have is a condition, and so long as this condition is true will the while loop continue to loop. So, the question is, how can i make it stop at a place where i want it to? The answer is simple, and you will be basically reusing code from the for loop question. What's different about the while loop though, is that you will use a boolean. A boolean means to be either true or false, which is what you have been dealing with previously, however this time you will be manually doing it instead of the program doing it for you.



while (con == true)

{



}



the loop will continue to loop so long as con is equal to true. Since boolean is either true or false, you have to make con false, and in order to do that you will need another condition. Your program has the condition that such and such amount of time must pass before the loop ends (5 years). Use a counter, and tally up the amount of iterations of the loop, and once it reaches 5 years (in months), reset con to false (and what is the name of the conditional statement you would use? I already mentioned it in this paragraph). This will stop the loop, and all that's left is to print out what the results are of the program.



Practice helps ALOT in programming, so i'd suggest taking a few hours every week to practice. it will really help in the long run.





Good luck,

~Barolb
Matthew
2013-03-25 17:20:27 UTC
This is NOT the place to get that advice as I doubt there would be any real programmers hanging around answers (although there I could be wrong). My suggestion would be to figure out how to answer the question on paper and then write the code.
anonymous
2015-10-24 03:30:12 UTC
CSE205 Object Oriented Programming and Data Structures Programming Project 1 :: 25 pts



1 Submission Instructions



Create a folder named where asuriteid is your ASURITE user id (for example, since my ASURITE user id iskburger2 my folder would be named kburger2) and copy all of your .java source code files to this folder. Do not copy the .class files or any other files. Next, compress the folder creating a zip archive file named .zip (mine would be named kburger2.zip). Upload .zip to the Project 1 dropbox by the project deadline. The deadline is 11:59pm Fri 28 Mar. Consult the online syllabus for the late and academic integrity policies.



2 Learning Objectives



1. Use the Integer wrapper class.

2. Declare and use ArrayList class objects.

3. Write code to read from, and write to, text files.

4. Write an exception handler for an I/O exception.

5. Write Java classes and instantiate objects of those classes.



3 Background



Let list be a nonempty sequence of nonnegative random integers, each in the range [0, 32767] and let n be the length of list, e.g.,

list = { 2, 8, 3, 2, 9, 8, 6, 3, 4, 6, 1, 9 }

where n = 12. List elements are numbered starting at 0. We define a run up to be a (k+1)-length subsequence listi, listi+1, listi+2, ..., listi+k, that is monotonically increasing (i.e., listi+j ≥ listi+j-1 for each j = 1, 2, 3, ..., k). Similarly, a run down is a (k+1)-length subsequence listi, listi+1, listi+2, ..., listi+k, that is monotonically decreasing (i.e., listi+j-1 ≤ listi+j for each j = 1, 2, 3, ..., k). For the above example list we have these runs up and runs down:

Runs Up list0 through list1 = { 2, 8 }; k = 1 list2 = { 3 }; k = 0

list3 through list4 = { 2, 9 }; k = 1

list5 = { 8 }; k = 0

list6 = { 6 }; k = 0

list7 through list9 = { 3, 4, 6 }; k = 2

list10 through list11 = { 1, 9 }; k = 1

Runs Down

list0 = { 2 }; k = 0

list1 through list3 = { 8, 3, 2 }; k = 2

list4 through list7 = { 9, 8, 6, 3 }; k = 3

list8 = { 4 }; k = 0

list9 through list10 = { 6, 1 }; k = 1

list11 = { 9 }; k = 0

We are interested in the value of k for each run up and run down and in particular we are interested in the total umber of

runs for each nonzero k, which we shall denote by runsk, 0 < k < n - 1. For the example list we have:

k runsk runs

1 4 { 2, 8 }, { 2, 9 }, { 1, 9 }, and { 6, 1 }

2 2 { 3, 4, 6, } and { 8, 3, 2 }

3 1 { 9, 8, 6, 3 }

4-11 0

Let runstotal be the the sum from k = 1 to n - 1 of runsk. For the example list, runstotal = 4 + 2 + 1 = 7.

Arizona State University Page 1

CSE205 Object Oriented Programming and Data Structures Programming Project 1 :: 25 pts

4 Software Requirements

Your program shall:

1. Open a file named p01-in.txt containing n integers, 1 ≤ n 1000, with each integer ≤ in [0, 32767]. There will be one

or more integers per line. A sample input file:

Sample p01-in.txt

2 8 3

2 9

8

6

3 4 6 1 9

2. The program shall compute runsk for k = 1, 2, 3, ..., n - 1.

3. The program shall compute runstotal.

4. The program shall produce an output file named p01-runs.txt containing runstotal and runsk for k = 1, 2, 3, ..., n - 1.

The file shall be formatted as shown in the example file below.

Sample p01-runs.txt

runs_total, 7

runs_1, 4

runs_2, 2

runs_3, 1

runs_4, 0

runs_5, 0

runs_6, 0

5. If the input file p01-in.txt cannot be opened for reading (because it does not exist) then display an error message on

the output window and immediately terminate the program, e.g.,

run program...

Sorry, could not open 'p01-in.txt' for reading. Stopping.

5 Software Design

Your program shall:

1. Contain a class named Main. This class shall contain the main() method. The main() method shall instantiate an

object of the Main class and call run() on that object.

// Main.java

public class Main {

public static void main(String[] pArgs) {

Main mainObject = new Main();

mainObject.run()

}

private void run() {

// You will start writing code here to implement the software requirements.

}

}

2. One of the primary objectives of this programming project is to learn to use the java.util.ArrayList class. Therefore,

you are not permitted to use 1D arrays. Besides, you will quickly discover that the ArrayList class is more

convenient to use than 1D arrays.

Arizona State University Page 2

CSE205 Object Oriented Programming and Data Structures Programming Project 1 :: 25 pts

3. ArrayList is a generic class meaning: (1) that it can store objects of any class; and (2) when an ArrayList object is

declared and instantiated we must specify the class of the objects that will be stored in the ArrayList. For this

project, you need to define an ArrayList that stores integers, but you cannot specify that your ArrayList stores ints

because int is a primitive data type and not a class. Therefore, you will need to use the java.lang.Integer wrapper

class:

ArrayList list = new ArrayList<():

int x = 1;

list.add(x); // Legal because of Java autoboxing.

4. You must write an exception handler that will catch the FileNotFoundException that gets thrown when the input

file does not exist (make sure to test this). The exception handler will print the friendly error message and

immediately terminate the Java program. To immediately terminate a Java program we call a static method named

exit() which is in the java.lang.System class. The exit() method expects an int argument. For this project, it does not

matter what int argument we send to exit(). Therefore, terminate the program this way:

try {

// Try to open input file for reading

} catch (FileNotFoundException pExcept) {

// Print friendly error message

System.exit(-1);

}

5. Your programming skills should be sufficiently developed that you are beyond writing the entire code for a program

in one method. Divide the program into multiple methods. Remember, a method should have one purpose, i.e., it

should do one thing. If you find a method is becoming complicated because you are trying to make that method do

more than one thing, then divided the method into 2, 3, 4, or more distinct methods, each of which does one thing.

6. Avoid making every variable or object an instance variable. For this project you shall not declare any instance

variables in the class. That is, all variables should be declared as local variables in methods and passed as

arguments to other methods when appropriate.

7. Format your code neatly. Use proper indentation and spacing. Study the examples in the book and the examples the

instructor presents in the lectures and posts on the course website.

8. Put a comment header block at the top of each method formatted thusly:

/**

* A brief description of what the method does.

*/

9. Put a comment header block at the top of each source code file—not just for this project, but for every project we

write—formatted thusly:

//********************************************************************************************************

// CLASS: classname (classname.java)

//

// DESCRIPTION

// A description of the contents of this file.

//

// COURSE AND PROJECT INFO

// CSE205 Object Oriented Programming and Data Structures, semester and year

// Project Number: project-number

//

// AUTHOR

// your-name (your-email-addr)

//********************************************************************************************************

Arizona State University Page 3

CSE205 Object Oriented Programming and Data Structures Programming Project 1 :: 25 pts

6 Pseudocode

Method Run() Returns Nothing

Declare and create an ArrayList of Integers named list

list ← ReadFile("p01-in.txt")

Declare and create an ArrayList of Integers named listRunsUpCount

Declare and create an ArrayList of Integers named listRunsDnCount

listRunsUpCount ← FindRuns(list, RUNS_UP)

listRunsUpCount ← FindRuns(list, RUNS_DN)

Declare and create an ArrayList of Integers named listRunsCount

listRunsCount ← Merge(listRunsUpCount, listRunsDnCount)

Output("p01-runs.txt", listRunsCount)

End Method Run

Method FindRuns(In: pList is ArrayList of Integers; int pDir is RUNS_UP or RUNS_DN) Returns ArrayList of Integers

listRunsCount ← arrayListCreate(pList.size(), 0)

Declare int varaibles i ← 0, k ← 0

While i < pList.size() - 1 Do

If pDir is RUNS_UP and pList element at i is ≤ pList element at i + 1 Then

Increment k

ElseIf pDir is RUNS_DN and pList element at i is ≥ pList element at i + 1 Then

Increment k

Else

If k 0 ≠ Then

Increment the element at index k of listRunsCount

k ← 0

End if

End If

Increment i

End While

If k 0 ≠ Then

Increment the element at index k of listRunsCount

End If

Return listRunsCount

End Method FindRuns

Method Merge(In: pListRunsUpCount, In: pListRunsDnCount) Returns ArrayList of Integers

listRunsCount ← arrayListCreate(pListRunsUpCount.size(), 0)

For i ← 0 to pListRunsUpCount.size() - 1 Do

Set element i of listRunsCount to the sum of the elements at i in pListRunsUpCount and pListRunsDnCount

End For

Return listRunsCount

End Method Merge

Method arrayListCreate(In: int pSize; In: int pInitValue) Returns ArrayList of Integers

Declare and create an ArrayList of Integers named list

Write a for loop that iterates pSize times and each time call add(pInitValue) on list

Return list

End Method arrayListCreate

Method Output(In: pFilename; pListRuns ArrayList of Integers) Returns Nothing

out ← open pFilename for writing

out.print("runs_total, ", the sum of pListRuns)

For k ← 1 to pListRuns.size() - 1 Do

out.print("runs_k, ", the element at index k of pListRuns)

End For

Close out

End Method Output

Arizona State University Page 4



SOLUTION : https://sellfy.com/p/1cjV
firpi
2016-11-07 15:01:39 UTC
My Asurite
Blues
2014-03-09 22:24:38 UTC
public class BankStatement

{

public int balanceInFiveYears(int startMoney)

{

int lastBalance=starMoney;



for(int i=0; i<12*5; i++)

{

lastBalance+=(25)/4;



}

return lastBalance;

}



}



//second program:



public class BankStatement2

{

public int balanceInFiveYears2(int startMoney)

{

int lastBalance=starMoney;

int i=0;

while(i<12*5)

{

lastBalance+=(25)/4;

i++;

}

return lastBalance;

}



}



//Main class to call both classes



public class MainClass

{

public static void main(String [] args)

{

BankStatement bast=new BankStatement();



System.out.println(bast.balanceInFiveYears(20));



BankStatement2 basty=new BankStatement2();



System.out.println(basty.balanceInFiveYears2(20));

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...