Results 1 to 10 of 10
 07232008, 05:28 AM #1Member
 Join Date
 Jul 2008
 Location
 Philippines!
 Posts
 4
 Rep Power
 0
Please help me solve these Java Problems
Hi guys, my name is Dianne and im from the philippines. ;)
Our teacher Gave us these problems in our java class and said she wont give us our prelims grades if we dont solve them. :(
Im really new with Java and im having a hard time solving them. I tried to but the problem says that i should use Methods and as i try to understand it, i just couldnt get it :confused:
So if any of you kind gentlemen could enlighten me, i would really appreciate it.:p
Its just two problems:
1.Write a Java Program that calculates the change for a cashier. The program requests the cost of the item. The user then types in the cost. The program then outputs the cost of the item including the sales tax (use 6% as the sales tax value). The program next requests and recieves the amount tendered by the customer. Finally, the program outputs a summary of all figures, including the amount of change due to the customer. Name the program as Change.java.
This program has the following attributes and methods:
double cost;
double tax;
double payment;
double getCost();
double getTax();
double getPayment();
void setCost (double x)
void setTax (double x)
void setPayment (double x)
2. Create a java program named Numbers whose main() method holds two integer variables. Assign values to the variables. Pass both variables to methods named sum() and difference(). Create the methods sum() and difference(); these compute the sum and difference between the values of two arguments, respectively. Each method should perform the appropriate computation and display the results. Name the program as Numbers.java.
Guys i really really tried hard to sovle these problems. But to no avail :mad:
This is my last resort. I need my grades :( Its really bothering me. I cant sleep :(
Help! :(
Dianne
 07232008, 05:39 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
What you have tried upto now. Any attempt you take?
Anyway here is a simple code for the second question.
Java Code:public class Numbers { public static void main(String[] args) { int valOne = 20; int valTwo = 37; Numbers num = new Numbers(); num.sum(valOne, valTwo); num.difference(valOne, valTwo); } private void sum(int i, int j) { System.out.println("Sum of " + i + " and " + j + " = " + (i + j)); } private void difference(int i, int j) { int diff = 0; if(i >= j) { diff = i  j; } else { diff = j  i; } System.out.println("Difference of " + i + " and " + j + " = " + diff); } }
 07232008, 06:41 AM #3Member
 Join Date
 Jul 2008
 Location
 Philippines!
 Posts
 4
 Rep Power
 0
Thank you for that 2nd problem! Thank you thank you!!!
I tried this with the first problem. I can certainly give what the problem is asking for but i just cant make one with Methods in it.
import java.io.*;
public class Change
{
public static void main(String args[]) throws IOException
{
BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
String input;
double cost=0;
double tax=0;
double payment=0;
double getCost=0;
double getTax=0;
double getPayment=0;
System.out.print("Please enter Item Cost:");
input=x.readLine();
cost=Double.parseDouble(input);
public void getTax(){tax = cost*getTax;
}
public void getCost(){ = cost+tax;
}
System.out.println("The cost of the item with included sales tax is "+getCost);
System.out.println("Please enter the amount of your payment");
input=x.readLine();
payment=Double.parseDouble(input);
if (payment<getCost)
System.out.println("Sorry but your payment is not enough");
getPayment = paymentgetCost;
System.out.println("The original cost of the item is "+cost);
System.out.println("The sales tax is 6%");
System.out.println("The item with added tax is no priced "+getCost);
System.out.println("Your change is "+getPayment);
}
}
 07232008, 07:09 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
You have define variables in wrong place. Do you know about the variable scope? That's the first thing I have seen in your code.
 07232008, 07:51 AM #5Member
 Join Date
 Jul 2008
 Location
 Philippines!
 Posts
 4
 Rep Power
 0
I dont have a clue with problem 1 using codes with Methods..
I keep getting errors like "illegal start of type.. etc" and stuff... everytime i try to incorporate a method.
:confused:
 07232008, 07:56 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 22
Yes you have done a big mistake there. You have define methods inside the main method. You can't do it, and that's why you get the error. And a lot more.
 07232008, 08:57 AM #7Java Code:
import java.io.*; public class ChangeRx { double cost=0; double tax=0.6; double payment=0; public static void main(String[] args) throws IOException { ChangeRx app = new ChangeRx(); BufferedReader x = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Please enter Item Cost:"); String input=x.readLine(); app.setCost(Double.parseDouble(input)); System.out.println("The original cost of the item " + "is " + app.getCost()); System.out.println("The sales tax is 6%"); app.setTax(app.getCost() * 0.06); System.out.println("The cost of the item with " + "included sales tax is " + (app.getCost() + app.getTax()) ); System.out.println("Please enter the amount of your payment"); input=x.readLine(); app.setPayment(Double.parseDouble(input)); if (app.getPayment() < app.getCost() + app.getTax()) System.out.println("Sorry but your payment is not enough"); double change = app.getPayment()  (app.getCost() + app.getTax()); System.out.println("Your change is " + change); x.close(); } double getCost() { return cost; } double getTax() { return tax; } double getPayment() { return payment; } void setCost(double cost) { this.cost = cost; } void setTax(double taxRate) { tax = taxRate; } void setPayment(double payment) { this.payment = payment; } }
 07272008, 11:58 AM #8Member
 Join Date
 Jul 2008
 Location
 Philippines!
 Posts
 4
 Rep Power
 0
To hardwired
Thank you so much. For giving me the program to # 1. That problem is due tomoro and you literally saved my butt. :o
I am really impressed by how you interpreted the problem and used every requirement to make the program. Its like you made it so that everything fits perfectly! Weh!
I dont think my teacher will believe if i say i did it w/o any help xD
Maraming Salamat Po!! (Thank you!!) :)
DianneLast edited by saculellav; 07272008 at 12:09 PM.
 07062014, 07:44 AM #9Member
 Join Date
 Jul 2014
 Posts
 1
 Rep Power
 0
Re: Please help me solve these Java Problems
Create an application named Numbers whose main() method holds two integer variables. Assign values to the variables. Pass both variables to methods named sum() and difference(). Create the methods sum() and difference(); they compute the sum of and difference between values of two arguments, respectively. Each method should perform the appropriate computation and display the results. Save the application as Numbers.java. Add a method named product() to the Numbers class. The product() method should compute the multiplication product of two integers, but not display the answer. Instead, it should return the answer to the calling method, which displays the answer. Save the application as Numbers.java.
i need to answer
 07062014, 09:25 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,290
 Blog Entries
 7
 Rep Power
 24
Similar Threads

Doubt Plzz Solve it
By ravjot28 in forum New To JavaReplies: 3Last Post: 06272008, 04:14 AM 
Can't solve error message while looping
By BHCluster in forum New To JavaReplies: 15Last Post: 04222008, 10:51 AM 
how to solve transformer exception
By java_arc in forum Advanced JavaReplies: 1Last Post: 04192008, 12:46 PM 
A great doubt in Java Applet,will u solve it!!
By anithababu in forum Java AppletsReplies: 6Last Post: 01272008, 02:20 PM 
Help mi solve my error
By Deon in forum New To JavaReplies: 3Last Post: 01112008, 06:26 AM
Bookmarks