Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Credit Card Charges pg 172 Chp 3 / Visual Basic 2015 RELOADED 6th ED / Diane Za

ID: 3889420 • Letter: #

Question

(Credit Card Charges pg 172 Chp 3 / Visual Basic 2015 RELOADED 6th ED / Diane Zak) Create an application that allows the user to enter the total monthly amount charged to his or her credit card for the following six categories: Merchandise, Restaurants, Gasoline, Total/Entertainment, Services, and Supermarkets. The application should calculate and display each month's total charges, as well as the total annual amount the user charged. The application should also calculate and display the percentage that each category contributed to the total annual amount charged.

Explanation / Answer

import java.util.Scanner; public class CreditCard { public static void main(String[] args) { long total; Scanner input = new Scanner(System.in); System.out.print("Please input your credit card number: "); long number = input.nextLong(); total = sumOfEvenPlaces(number) + sumOfOddPlaces(number); System.out.print(total); if(isValid(total)) { System.out.println("The length of your card number is: " + getSize(number)); System.out.println("This card is valid."); } else { System.out.println("The length of your card number is: " + getSize(number)); System.out.println("Your card is invalid."); } } public static boolean isValid(long total) { if (total % 10 == 0) { return true; } return false; } public static int sumOfEvenPlaces(long number) { int sum = 0; int remainder; number %= 10; while (number % 10 != 0 || number / 10 != 0) { remainder = (int)(number % 10); sum = sum + getDigit(remainder * 2); number /= 100; } return sum; } public static int getDigit(int number) { if (number 9) return (number % 10 + number / 10); return number; } public static int sumOfOddPlaces(long number) { int sum = 0; int remainder; number /= 10; while(number % 10 != 0 || number / 10 != 0) { remainder = (int)(number % 10); sum = sum + getDigit(remainder * 2); number /= 100; } System.out.println(sum); return sum; } public static int getSize(long number) { int len = 0; while (number >= 10) { number /= 10; len++; } return len; } }