Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I need some guidance as how can I optimize the below code. As one can see , i am creating a new object everytime to set each value. How can I do it in a better way
Balance[] balance = new Balance[3];
CurAmt curAmt = new CurAmt();
Balance bal = new Balance();
bal.setBalType("currentBalance");
curAmt.setCurCode(entry.getValue().get(0).getAs("CURRENT_BALANCE_CURRENCY_CODE"));
curAmt.setContent(entry.getValue().get(0).getAs("CURRENT_BALANCE"));
bal.setCurAmt(curAmt);
balance[0] = bal;
CurAmt curAmt1 = new CurAmt();
Balance bal1 = new Balance();
bal1.setBalType("availableBalance");
curAmt1.setCurCode(entry.getValue().get(0).getAs("AVAILABLE_BALANCE_CURRENCY_CODE"));
curAmt1.setContent(entry.getValue().get(0).getAs("AVAILABLE_BALANCE"));
bal1.setCurAmt(curAmt1);
balance[1] = bal1;
CurAmt curAmt2 = new CurAmt();
Balance bal2 = new Balance();
bal2.setBalType("interestEarnedYtd");
curAmt2.setCurCode(entry.getValue().get(0).getAs("YTD_CURRENCY_CODE"));
curAmt2.setContent(entry.getValue().get(0).getAs("TD_AMOUNT"));
bal2.setCurAmt(curAmt2);
balance[2] = bal2;
Obj.setBalance(balance);
If you are talking about code performance, I can only see one optimization. You evaluate this 6 times, and the JIT compiler probably can't optimize it:
entry.getValue().get(0)
Do it once and assign the value to a temporary variable. (It is unclear if this will make a significant difference. And you were just doing this for performance reasons, you would be advised to profile your application first. There is no point in optimizing application code that is not a performance hotspot.)
If you are talking about programmer efficiency (i.e. code readability) I can see a block of code that is repeated 3 times with different parameters. You can turn it into a method; e.g.
private Balance createBalance(Value v, String type, String currency, String balance) {
CurAmt curAmt = new CurAmt();
Balance bal = new Balance();
bal.setBalType(type);
curAmt.setCurCode(value.getAs(currency));
curAmt.setContent(value.getAs(ballance));
bal.setCurAmt(curAmt);
return bal;
}
then call it with the appropriate parameters.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I'm learning java now, sorry if you didn't get it.
Here is the "money transfer" method, it works with constructor in other class.
The code does not end by itself, it constantly returns.
I need to save these parameters (username, transfer amount) and execute this later.
public static String moneyTransfer() {
System.out.println("Enter the name to whom you want to transfer funds");
String nametransfer = scanner.nextLine();
String nametransf = scanner.nextLine();
System.out.println("Enter the amount to transfer to the user " + nametransfer);
int transfermoney = scanner.nextInt();
Moneytransfer moneytr = new Moneytransfer(nametransf, transfermoney);
int resulttransfer = balance-transfermoney;
System.out.println("You transferred "+transfermoney+" to user: "+nametransf);
System.out.println("balance: "+resulttransfer);
return startPanel();
}
idk(
You can store them with a List in package java.util.
As you are new to programming, you might not understand what I am talking about. Just keep learning and you will realise.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I would like to put the following to a for loop. How do I do this since we haven't studied arrays yet. My instructor would like me to put the quarters 1-4 to a for loop.
quarter = 1;
interest = principal * quarterlyRate; // item 5
finalAmount = principal + interest;
System.out.printf("%6s%8d%16.2f%30.2f%n", year, quarter, interest, finalAmount);```
Maybe this is what you want:
# looping here
for (int i=0; i<4; i++) {
interest = principal * quarterlyRate; // item 5
finalAmount = principal + interest;
System.out.printf("%6s%8d%16.2f%30.2f%n", year, i, interest, finalAmount);```
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a class with data member:
private static final int DEFAULT_SIZE = 10;
private Stack<String> myStack = new Stack<>();
// want to add more element to the stack based on demand - but add incremental demand
public void addCapacity(int incremental) {
int diff = incremental - DEFAULT_SIZE;
myStack.forEach((diff) -> {
myStack.push("something");
});
}
The idea was to see if this forms a use case for lambda functions. But it will not allow me as it is expecting a boolean in place of diff. Is lambda forEach a use-case here?
Since Stack extends Vector, if you wanted to increase the capacity, then you could have used ensureCapacity (in Vector)
myStack.ensureCapacity(minCapacity);
If you wanted to do myStack.push("something") diff times, then you could have used:
IntStream.range(0, diff).forEach(i -> myStack.push("something"));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The code doesn't work right the program runs both if statements and the total never comes out right. I can't figure out whats wrong. what changes do i need to make for this to work? the directions are below.
The cost to become a member of a fitness center is as follows: (a) the Senior
citizens discount is 30%; (b) if the membership is bought and paid for 12 or
more months in advance, the discount is 15%; or (c) if more than 5 personal
training sessions are purchased, the discount on each session is 20%.
Write a menu driven program that determines the cost of a new membership.
Your program must contain a method that displays the general information about
the fitness center and its charges, a method to get all the necessary information
to determine the membership cost, and a method to determine the membership
cost. Use appropriate parameters to pass information in and out of a method.
what methods should i use?
double grossdiscount1,grossdiscount2,grossdiscount3;
double grossprice1,grossprice2,grossprice3;
//end result of box calculation
double answerbox1,answerbox2,answerbox3;
double answerbox1b,answerbox2b,answerbox3b;
//Jtext inputs
double box1,box2,box3;
double discount1 = 0.30 ;
double discount2 = 0.20 ;
double discount3 = 0.15 ;
// prices PT=personal training MT=montly price
double pricePT =50.00;
double priceMT =100.00;
box1 = Double.parseDouble(jTextField1.getText());
box2 = Double.parseDouble(jTextField2.getText());
box3 = Double.parseDouble(jTextField3.getText());
// i brought these out of the if statement because the program won't run without
// them being stated
answerbox1b=box1*100;
grossdiscount1=(box1*priceMT)*discount3;// the amount saved
grossprice1=(box1*priceMT);
answerbox1=(grossprice1-grossdiscount1);
answerbox2b=(box2*pricePT);
grossdiscount2=(box2*pricePT)*discount2;// the amount saved
grossprice2=(box2*pricePT);
answerbox2=(grossprice2-grossdiscount2);
double total = answerbox1+answerbox2+answerbox1b+answerbox2b;
grossdiscount3=(total*discount3);// the amount saved
grossprice3=total;
answerbox3=(grossprice3-grossdiscount3);
if(box1<11 )
{
answerbox1b=box1*100;
}
else if(box1>12)
{
grossdiscount1=(box1*priceMT)*discount3;// the amount saved
grossprice1=(box1*priceMT);
answerbox1=(grossprice1-grossdiscount1);
}
if(box2<5 )
{
answerbox2b=(box2*pricePT);
}
else if(box2>=5)
{
grossdiscount2=(box2*pricePT)*discount2;// the amount saved
grossprice2=(box2*pricePT);
answerbox2=(grossprice2-grossdiscount2);
}
if(box3==1 )
{
grossdiscount3=(total*discount3);// the amount saved
grossprice3=total;
answerbox3=(grossprice3-grossdiscount3);
}
else if(box3==0);
{
}
jTextField4.setText(String.valueOf(total));
The else if statements are not written correctly. Remove the ; for a correct flow.
And I also recommend you to check any of the threads related to comparing doubles in Java
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
can anyone tell me how can i connect one string with a group of strings and each string have certain value.
for exampe:
String car = new String()
//i give it a value of say 10 dollars
and then i have this group of Strings/Objects
for example:
String frontWindow = new String()
//i set a value for it
String backWindow = new String()
//i set a value for it
String wheel = new String()
//i set a value for it
And i want to calculate the sum of the car with some or all of the string above.I thought about usint the print method but then i would be able to choose only all strings.So does anyone know how can i make this work in the console output?
That's not very clear, but it seems like you want to make a Car class and access variables inside of it.
public class Car{
public String frontWindow, wheel;
public Car(String frontWindow, String wheel){
this.frontWindow = frontWindow;
this.wheel = wheel
}
}
then, from inside an other class you can access it like this:
Car myCar = new Car("this is my front window", "this is my wheel");
System.out.println(myCar.wheel);
if you want those variables to hold values instead of strings, make them "double"s or "int"s.
Look up the difference between storing Strings (text) and Numbers (int, double, long, float, etc.)
Also look up Object Oriented programming for how that all works. Try a google search for "how to create a new object in java", or something like that. Then you also might be able to use getters and setters for those variables you want.
You can combine strings like so:
String totalSum = car + frontWindow + backWindow + wheel;
However, given this example:
String car = "10.00";
String frontWindow = "2.00";
String backWindow = "2.00";
String wheel = "1.00";
They would add together end-to-end and be like so: "10.002.002.001.00"
int and double are actually meant to have number operations done to them instead of String.
int i = 5; // int's are whole numbers, no decimals allowed
int j = 3;
int total = i + j; //Total is 8
double d1 = 1.1;
double d2 = 2.5;
double dTotal = d1 + d2; //Total is 3.6
You can still print them out to the console by doing System.out.println(dTotal);
Probably out of scope, but if you plan on dealing with money in a serious business app, you should check into the BigDecimal class because of tiny rounding errors that can occur with doubles and floats.