#2 Use JOption please Showchar Method Write a method na a String object and an i
ID: 3591588 • Letter: #
Question
#2 Use JOption please
Showchar Method Write a method na a String object and an integer. The integer argument is a character position within the med showchar. The method should accept two arguments: a reference to string, with the first character being at position 0. When the method executes, it should display the character at that character position. Here is an example of a call to the method showchar("New York", 2) In this call, the method will display the character w because it is in position 2. Demonstrate the method in a complete program. 2. Retail Price Calculator Write a program that asks the user to enter an item's wholesale cost and its markup percent- age. It should then display the item's retail price. For example: ideoNote e Retail If an item's wholesale cost is 5.00 and its markup percentage is 100 percent, then the Price item's retail price is 10.00. culatorIf an item's wholesale cost is 5.00 and its markup percentage is 50 percent, then the roblem item's retail price is 7.50. The program should have a method named aleulatoRetal that receives the wholesale cost and the markup percentage as arguments, and returns the retail price of the item hat receives the wholesale 3. Rectangle Area-Complete the Program If you have downloaded the book's source code from www.pearsonhighered.com/gaddis, you will find a partially written program named AreaRectangle.java in this chapter's source code folder. Your job is to complete the program. When it is complete, the program will ask the user to enter the width and length of a rectangle, and then display the rectan- gle's area. The program calls the following methods, which have not been written: getLength-This method should ask the user to enter the rectangle's length, and thenExplanation / Answer
/*answer for the second question only as you requested*/
//RetailPriceCalculator.java
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class RetailPriceCalculator {
public static void getInput() {
try {
JPanel myPanel;
JTextField wholesale = new JTextField(5);
JTextField markup = new JTextField(5);
myPanel = new JPanel();
myPanel.add(new JLabel("wholesale cost:"));
myPanel.add(wholesale);
myPanel.add(Box.createHorizontalStrut(15));
myPanel.add(new JLabel("markup cost:"));
myPanel.add(markup);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Enter wholesale price and markup cost",
JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
float wholesale_price = Float.parseFloat(wholesale.getText());
int markup_percent = Integer.parseInt(markup.getText());
if (markup_percent > 100 || markup_percent < 0) { /* invalid percentage */
throw new Exception();
} else {
JOptionPane.showMessageDialog(myPanel,
calculateRetail(wholesale_price, markup_percent)); /* displaying the result */
}
}
} catch (Exception e) {
System.err.println("Error, enter right values only"); /* invalid input */
getInput();
}
}
public static void main(String ar[]) {
getInput(); /* getting user input & finding the result */
}
public static float calculateRetail(float wholesale_cost, int markup_percent) { /* function to calculate the retail cost */
return wholesale_cost
+ (wholesale_cost * ((float) markup_percent / 100));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.