Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is a code that has a test code that has been formatted using String.format(). How could I alter the code to vertically align the

Below is a code that has a test code that has been formatted using String.format(). How could I alter the code to vertically align the code using the Util.padleft() method instead of String.format()? In Java please. The Code to the Util method is,

public class Util { public static String padLeft(String s, int width) { String r = s; while (r.length() < width) r = " " + r; return r; } } // end class

Start of Code:

import java.util.Random; public class LotteryTicket { private static int MAX_NUMBER = 49; private static int MIN_NUMBER = 1; public static int NUM_QTY = 6; private int[] numbers; private int ticketId; public LotteryTicket(int ticketIdIn) { this.ticketId = ticketIdIn; this.numbers = new int[NUM_QTY]; chooseRandomNumbers(); } private void chooseRandomNumbers() { Random rand = new Random(); int i = 0; while (i < NUM_QTY) { numbers[i] = rand.nextInt(MAX_NUMBER - MIN_NUMBER + 1) + MIN_NUMBER; // If It's Not Duplicate Increment I if (!duplicateNumber(i)) { i++; } } } public int countWinningNumbers(int[] winningNumbers) { int numMatches = 0; for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < winningNumbers.length; j++) { if (numbers[i] == winningNumbers[j]) { numMatches++; break; } } } return numMatches; } private boolean duplicateNumber(int i) { for (int j = 0; j < i; j++) { if (numbers[j] == numbers[i]) { return true; } } return false; } public int[] getNumbers() { return numbers; } public int getTicketId() { return ticketId; } @Override public String toString() { StringBuilder sb = new StringBuilder(String.format("%d:", ticketId)); for (int i = 0; i < numbers.length; i++) { sb.append(String.format("%3d", numbers[i])); } return sb.toString(); } } public class LotteryDraw { private static double[] prizeAmount = { 0, 0, 10, 100, 1000, 100000, 1000000 }; private int ticketQty; private LotteryTicket[] tickets; private LotteryTicket winningNumbers; public LotteryDraw(int maxTickets) { ticketQty = 0; tickets = new LotteryTicket[maxTickets]; winningNumbers = new LotteryTicket(0); } public boolean addTicket(LotteryTicket t) { if (ticketQty < tickets.length) { tickets[ticketQty] = t; ticketQty++; return true; } return false; } public LotteryTicket getTicket(int index) { return tickets[index]; } public double getPrizeAmount(int n) { return prizeAmount[n]; } public int[] getWinningNumbers() { return winningNumbers.getNumbers(); } } import java.text.NumberFormat; import java.util.Locale; public class LotteryDrawTest { public static void main(String[] args) { int maxTickets = 20; int numTickets = 10; LotteryDraw lotteryDraw = new LotteryDraw(maxTickets); for (int i = 0; i < numTickets; i++) { LotteryTicket ticket = new LotteryTicket(i + 1001); lotteryDraw.addTicket(ticket); } int[] winningNumbers = lotteryDraw.getWinningNumbers(); System.out.print("Winning Numbers: "); for (int i = 0; i < winningNumbers.length; i++) { System.out.printf("%3d", winningNumbers[i]); } NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US); System.out.printf(" %15s%18s%9s ", "Tickets", "#Matched", "Prize"); System.out.println("======================= ======== ========="); // Show Ticket Info for (int i = 0; i < numTickets; i++) { LotteryTicket ticket = lotteryDraw.getTicket(i); int numMatches = ticket.countWinningNumbers(winningNumbers); double prize = lotteryDraw.getPrizeAmount(numMatches); System.out.printf("%s%7d%12s ", ticket, numMatches, formatter.format(prize)); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting Information Systems

Authors: George H. Bodnar, William S. Hopwood

11th Edition

0132871939, 978-0132871938

More Books

Students also viewed these Programming questions