Results 1 to 8 of 8
  1. #1
    saculellav is offline Member
    Join Date
    Jul 2008
    Location
    Philippines!
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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);
        }
    
    }

  3. #3
    saculellav is offline Member
    Join Date
    Jul 2008
    Location
    Philippines!
    Posts
    4
    Rep Power
    0

    Default

    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);
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  5. #5
    saculellav is offline Member
    Join Date
    Jul 2008
    Location
    Philippines!
    Posts
    4
    Rep Power
    0

    Default

    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:

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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;
        } 
    }

  8. #8
    saculellav is offline Member
    Join Date
    Jul 2008
    Location
    Philippines!
    Posts
    4
    Rep Power
    0

    Default

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

    Dianne
    Last edited by saculellav; 07-27-2008 at 12:09 PM.

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •