I was a bit late with this.
Also: I understand there was only one TA available on Wednesday afternoon. That was due to special circumstances, normally there will be three TAs available. Sorry to anyone who wasn't able to have their question answered ...
What is the format of the e-mail for your critique.
ReplyDeleteI didn't get a reply for my precious e-mail.
Thanks.
For Lab 4, for the merge method, are the two arrays passed to the method already sorted in non-decreasing order?
ReplyDeletenever mind. The arrays A and B are sorted in non-decreasing order.
ReplyDeleteI posted this earlier under the "Small change to sign-in policy" blog because this one wasn't created yet. I guess nobody saw it so here it is again:
ReplyDeleteIn Lab5, for the LineOfCredit class, the method withrawal(double amount) has the following description in the API:
Withdraws the given amount from the line of credit. No fee is charged. If the final balance is less than the credit limit, the withdrawal is cancelled and the balance is unchanged. Returns the final balance after the withdrawal.
This line in particular is confusing for me: If the final balance is less than the credit limit, the withdrawal is cancelled and the balance is unchanged.
The default credit limit is 10,000 and the default balance is 0. This forces the balance to be over 10,000 dollars to be able to make a withdrawal, which seems strange to me and it does not result in the same output as the example output given in Lab5.
Can someone clarify this for me, please?
hey guys
ReplyDeleteCan someone please help me with the longestrun method in lab4? I tried but my code is not giving the correct answer.
Thanks
@ 4:05
ReplyDeleteIt says the final balance which includes the current balance and the passed amount.
if(super.getBalance() + amount < limit)
{
return super.getBalance();
}
else
{
double d = super.setBalance(super.getBalance() - amount);
return d;
}
this is what I did and it worked fine for me.
@8:12
ReplyDeleteI know what the API says and my code works just fine. My problem is with the logic of the statement. Look at your own code, you wrote:
if(super.getBalance() + amount < limit)
{
return super.getBalance();
}
The default limit is $10,000 so the balance and the amount will almost always be less than the limit which means you cannot make the withdrawal. So if I have $100 dollars in my account then I can't withdraw $50 because 100 + 50 = 150 which is a lot less than 10,000. Does that make sense???
You should run your code and check if you get the same output as the sample output.
@6:50
ReplyDeleteRun through the array with a for-loop and compare the first element with the second, the second with the third, etc. Take note of when a new element is larger than the previous element (This increases the "run"). When you reach an element that is not larger than the previous element, store the number of "runs" and start counting from zero again and repeat the process until the array is completed. After this, return the largest or "longest" of these runs.
I know it sounds wordy, but there's really not much else I can say. It's pretty much just one for-loop and a few if-else statements. The key is to store two variables from the array so that you can compare them and keep track of the "runs" then return the longest run.
10:30: Read the API carefully. In the three-parameter constructor, we have "The balance and credit limit of a line of credit are normally negative, indicating the amount borrowed from the bank". In the default constructor the credit limit starts out as "-10000" i.e. negative 10000.
ReplyDeleteIf the balance goes less than -10000, then the customer exceeds his/her credit limit.
Hope that helps.
Wow, I can't believe I missed that...
ReplyDeleteYa, that was my mistake, sorry, and thank you for the clarification.
I'm having trouble implementing the method merge for Lab4.
ReplyDeleteI don't know the algorithm for it. Can someone give me a hint?
Thanks.
=(
@Woody:
ReplyDeleteThere are many ways of doing it. The way I prefer was simply taking the smallest number from either the two arrays, and then add it to the third, new array. This only works if both arrays are already sorted, which they are.
i.e.
bold is from, italic is to
====
Array1=[1,3,4]
Array2=[2,7]
Array3=[]
====
Array1=[3,4]
Array2=[2,7]
Array3=[1]
====
Array1=[3,4]
Array2=[7]
Array3=[1,2]
====
Array1=[4]
Array2=[7]
Array3=[1,2,3]
====
Array1=[]
Array2=[7]
Array3=[1,2,3,4]
====
Array1=[]
Array2=[]
Array3=[1,2,3,4,7]
Of course when you actually writes the code you don't need to get rid of the elements from the first two array. I only did so in the demonstration to make it clear where the values are coming from.
Please excuse any typos, it's a Friday night.
@ Yuen
ReplyDeleteThanks. I worked on this for a few hours.
=)
@w, I'm glad I could help. :)
ReplyDeleteDid you guys know that there is a built in sort method in the java.util.Arrays class?
ReplyDeleteWe're allowed access to the entire java library during a labtest. You could just add the two passed arrays together and then use Arrays.sort() and you're done.
It might be fun trying to come up with custom sort methods, but I just thought I'd let you know if you're really frustrated with writing the merge method that there is a much simpler way.
Also, as a side note, I've read that it's not really common for programmers in industry to write custom methods when very efficient and well established ones already exist. It's kind of like reinventing the wheel.
I hope this helps.
I'm still working on that method.
ReplyDelete=(
I don't know but is it just me or did everyone had problems with Arrays? Professor I was wondering if you could go over Arrays one more time please. I completed my lab in about 4 hours :'(
ReplyDeleteI gave up on that. I did that method for the whole day.
ReplyDelete@Anonymous 2:26 PM
ReplyDeleteOh, I get what you meant now. I think you meant the lab, not the labtest.
My method only works for some cases. I don't want to spend too much time on this. I got confused about going through the 2 arrays. I wasn't sure about working with the end of the 2 arrays.
I wasted my time if you said I can use Array.sort()
Now, I spent the night doing this.
@Woody
ReplyDeleteI'm sorry to hear that, but if it makes you feel any better I spent a few hours on this myself until I realized I was over complicating the problem.
I'll add my code for the merge method here in case you want to see it:
Note: you must import java.util.arrays;
public static int[] merge(int[] A, int[] B)
{
int totalLength = A.length + B.length;
int[] C = new int[totalLength];
for(int i = 0; i < A.length; i++)
{
C[i] = A[i];
}
for (int i = 0; i < B.length; i++)
{
C[i + A.length] = B[i];
}
Arrays.sort(C);
return C;
}
I'm sure that there are many other solutions, but this is pretty simple and it also works even if the two passed arrays weren't already sorted. (Although the API says they will be)
Sorry, just a small correction.
ReplyDeleteI wrote: you must import java.util.arrays;
It should be: import java.util.Arrays;
It must be a capital letter "A".
Is this right?
ReplyDeleteIf your parent class doesn't have a 0-argument constructor, and your child class' constructor doesn't call the parent's constructor, there will be an error?
(page 436)
Hello all,
ReplyDeleteI have solution for Lab 4 posted here :
Long version
Short version
The long version contains many explanations, and is written in a form that (hopefully) easier to understand. However, they might not very efficient in term of time and memory used.
The short version is the most efficient solution that I can come up with. There is some notes in each method that explains the trick. This version is intended to show that there exist a "short and simple" solution for each method.
Why we bother writing our own code while there exist Arrays.Sort() in JAVA ?
This is so that you can understand what's going on behind the scene, not just using what other people has made. Who knows, one day you might get the chance write something for JAVA's standard library that other people can use, rather than using what they wrote :)
Also, the benefit of understanding the logic is, when you get a "similar but not exactly the same" problem, then you know how to write an efficient modified-algorithm for it.
@Woody
ReplyDeleteYa, it pretty much explains it all on the page you cited.
If you have a child class and in its constructor you don't call any parent class constructor then Java will automatically look for the parent's no-parameter constructor and call it.
Let's say that in your parent class you wrote a 3-parameter constructor, but didn't want to write a no-parameter constructor. Then Java will still look for the no-parameter constructor regardless of the fact that you wrote a 3-parameter constructor. Because Java won't find it, it will cause a compile-time error.
I don't think you will ever run into this, because usually you write all child class constructors with a call to some parent constructor so Java won't have to look for a no-parameter constructor.
For the lineOfCredit class, the balance needs to be positive.
ReplyDeleteIt herits BankAccount's balance. It doesn't check if balance is positive in the BankAccount class.
to submit Lab5 exercise, use "submit 1030 Lab5 ..." instead of "submit 1030 lab5 ..." (the Lab5 is using capital letter).
ReplyDelete@8:12
ReplyDeleteI'm wondering about this line of code:
double d = super.setBalance(super.getBalance() - amount);
According to the API, the return type of BankAccount.getBalance() is supposed to be void... is the API wrong?
I don't understand how "Line of Credit"s work. Could anybody help me understand these two methods: lineOfCredit.withdraw & lineOfCredit.deposit
For deposit, API says, "Deposits the given amount into the line of credit. No fee is charged. If the deposit amount is negative, the deposit does not happen and the balance is unchanged. Returns the balance at the end of the deposit. "
if (amount >= 0)
{
this.limit += amount;
super.setBalance(super.getBalance() + amount);
}
Is this correct?
@ Dennis
ReplyDeleteI'll try to explain this:
The method setBalance in BankAccount is void because it doesn't return a value. I just sets a value to BankAccount's attribute called 'balance'.
The method getBalance in BankAccount returns the balance of any child bank class.
You can't do this:
double d = super.setBalance(super.getBalance() - amount);
setBalance() is a void method.
You can do this:
super.setBalance(super.getBalance() - amount);
double d = super.getBalance();
I don't know what line of credit is, but it's like this:
if(amount >= 0)
super.setBalance(super.getBalance() + amount);
@Woody
ReplyDeleteAh I made a mistake in assuming the limit gets modified.
So in the Withdraw method we should simply have
if (super.getBalance() - amount > this.getLimit())
super.setBalance(super.getBalance() - amount);
right?
public SavingAccount(SavingAccount s)//This is from lab5.
ReplyDelete{
super(s);
}
When you use super(s), isnt that equivalent to saying SavingsAcccount s = new BankAccount(s);
So then s is really a BankAccount and not a SavingsAccount. Either what i'm thinking is wrong or the way I am doing it is wrong.
Is there a solution to Lab5 somewhere?
What I said above is assuming that BankAccount is not abstract. Even if it is Abstract though, doesn't that mean that after late binding you are technically instantiating BankAccount?
ReplyDeleteI think is get it actually, because you create the object as SavingsAccount s = new SavingsAccount(...); which will make it automatically cast to a savings account. I hope nobody receives notifications from this blog..... Sorry for spamming if you are notified.
ReplyDelete