Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008, 06:28 AM
Member
 
Join Date: Jul 2008
Location: Philippines!
Posts: 4
saculellav is on a distinguished road
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

So if any of you kind gentlemen could enlighten me, i would really appreciate it.

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
This is my last resort. I need my grades Its really bothering me. I cant sleep

Help!

-Dianne
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-23-2008, 06:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,556
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
What you have tried upto now. Any attempt you take?

Anyway here is a simple code for the second question.

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); } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-23-2008, 07:41 AM
Member
 
Join Date: Jul 2008
Location: Philippines!
Posts: 4
saculellav is on a distinguished road
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 = payment-getCost;

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);
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-23-2008, 08:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,556
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-23-2008, 08:51 AM
Member
 
Join Date: Jul 2008
Location: Philippines!
Posts: 4
saculellav is on a distinguished road
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.

Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-23-2008, 08:56 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,556
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-23-2008, 09:57 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-27-2008, 12:58 PM
Member
 
Join Date: Jul 2008
Location: Philippines!
Posts: 4
saculellav is on a distinguished road
To hardwired

Thank you so much. For giving me the program to # 1. That problem is due tomoro and you literally saved my butt.

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!!)

Dianne

Last edited by saculellav : 07-27-2008 at 01:09 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubt Plzz Solve it ravjot28 New To Java 3 06-27-2008 05:14 AM
Can't solve error message while looping BHCluster New To Java 15 04-22-2008 11:51 AM
how to solve transformer exception java_arc Advanced Java 1 04-19-2008 01:46 PM
A great doubt in Java Applet,will u solve it!! anithababu Java Applets 6 01-27-2008 02:20 PM
Help mi solve my error Deon New To Java 3 01-11-2008 06:26 AM


All times are GMT +3. The time now is 02:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org