Hello, I need help with the asignment below. It has to do with Utilizing loops i
ID: 3909265 • Letter: H
Question
Hello, I need help with the asignment below. It has to do with Utilizing loops in Java programming
.
This is what i have so far, but it does not let me input information like the image above asks me to.
package schoolcontribution;
public class SchoolContribution {
double total = 0.0;
static int days = 30;
double day[] = new double[30];
int dayNumber = 1;
public SchoolContribution()
{
total = 0.0;
}
public void contribute(double amount, int currentDay)
{
dayNumber = currentDay;
day[--currentDay] = amount + day[currentDay];
}
public boolean hasMoreTime()
{
return (dayNumber <= 30)?true:false; // if and else return
}
public boolean needMoreMoney()
{
return (total < 5000)?true:false;
}
public double totalContribution()
{
for (int dayCounter = 0; dayCounter < 30; dayCounter++ )
{
total = total + day[dayCounter];
}
return total;
}
public String toString()
{
return String.valueOf(total);
}
}
package schoolcontribution;
public class TestContribution {
public static void main(String[] args) {
SchoolContribution object1 = new SchoolContribution();
object1.contribute(200, 1);
object1.contribute(400, 2);
object1.contribute(200, 2);
object1.contribute(50, 1);
object1.contribute(23, 6);
object1.contribute(47, 4);
object1.contribute(80, 10);
System.out.println(object1.totalContribution());
}
}
Explanation / Answer
here is your program : ---------->>>>>>>>>
public class SchoolContribution {
double total = 0.0;
static int days = 30;
double day[] = new double[30];
int dayNumber = 1;
public SchoolContribution()
{
total = 0.0;
for(int i = 0;i<30;i++)
day[i] = 0.0;
}
public void contribute(double amount, int currentDay)
{
dayNumber = currentDay;
day[--currentDay] = amount + day[currentDay];
}
public boolean hasMoreTime()
{
return (dayNumber <= 30)?true:false; // if and else return
}
public boolean needMoreMoney()
{
return (total < 5000)?true:false;
}
public double totalContribution()
{
total = 0;
for (int dayCounter = 0; dayCounter < 30; dayCounter++ )
{
total = total + day[dayCounter];
}
return total;
}
public String toString()
{
return String.valueOf(total);
}
}
----------------------------------------------------------------
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
public class TestContribution {
public static void main(String[] args) {
SchoolContribution object1 = new SchoolContribution();
double contri = 0.0;
int st = 0;
double[] arr1 = new double[30];
double[] arr2 = new double[30];
int count = 0;
for(int i = 1;i <= 30;i++){
for(int j = 0;j<3;j++){
contri = Double.parseDouble(JOptionPane.showInputDialog("Make Contribution "));
object1.contribute(contri,i);
arr2[i-1] = arr2[i-1] + contri;
if(object1.totalContribution() >= 5000.0){
st = 1;
break;
}
}
count++;
arr1[i-1] = object1.totalContribution();
if(object1.totalContribution() >= 5000.0){
st = 1;
break;
}
}
String s = "Day DayContribution TotalContribution";
for(int i = 0;i<count;i++){
s += " "+(i+1)+" $"+String.format("%6.2f",arr2[i])+" $"+String.format("%6.2f",arr1[i]);
}
if(st == 0){
s += " we did not make the Target. Sorry, we can not go on the trip.";
}else{
s += " Congrats,you made it.";
}
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(300, 300));
JTextArea area = new JTextArea();
area.setText(s);
area.setEditable(false);
JScrollPane scrollPane = new JScrollPane(area,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(300,300));
panel.add(scrollPane);
int result = JOptionPane.showConfirmDialog(null, panel,
"School Contribution", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
//System.out.println(txtName.getText() + ", " + txtAddress.getText());
} else {
//System.out.println("Canceled");
System.exit(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.