Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

(Java program問題)

Write a program that reads the subtotal and the gratuity rate, and computes the gratuity and total. For

example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity

and $11.5 as total.

1 Answer

Rating
  • ?
    Lv 5
    1 decade ago
    Favorite Answer

    Please try following code:

    import java.io.*;

    public class CalTotal

    { public static void main(String[ ] args)

    { PrincipalRate PR = new PrincipalRate( );

    PR.SetPrincipalRate( 10, 0.15 );

    System.out.println( "Gratuity : " + PR.GetGratuity( ) );

    System.out.println( "Total : " + PR.GetTotal( ) );

    }

    }

    class PrincipalRate

    { private int Principal = 0;

    private double Rate = 0.0;

    public void SetPrincipal( int iPrincipal )

    { Principal = iPrincipal;

    }

    public void SetRate( double dRate )

    { Rate = dRate;

    }

    public void SetPrincipalRate( int iPrincipal, double dRate )

    { SetPrincipal( iPrincipal );

    SetRate( dRate );

    }

    public int GetPrincipal( )

    { return Principal;

    }

    public double GetRate( )

    { return Rate;

    }

    public double GetGratuity( )

    { return Principal * Rate;

    }

    public double GetTotal( )

    { return Principal * ( 1 + Rate );

    }

    }

    OK?

Still have questions? Get your answers by asking now.