Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I had my code fixed to display with string formatting but now it displays like this and I cant seem to find the issue

Hello, I had my code fixed to display with string formatting but now it displays like this and I cant seem to find the issue in the code pertaining to it. Here is how the code prints (it is supposed to display a formatted calendar when given month and year)(This is written in the language Kotlin):

February 2023

---------------------------------------

Sun Mon Tue Wed Thr Fri Sat

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Here is the code:

fun dayOfWeek(month: Int, year: Int): Int { val q = 1 val m = if (month < 3) month + 12 else month val k = year % 100 val j = year / 100 val h = (q + (13 * (m + 1)) / 5 + k + (k / 4) + (j / 4) + (5 * j)) % 7 return h }

fun main() { val month = 2 val year = 2023

// Array of month names val months = arrayOf("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

// Array of number of days in each month val daysInMonth = arrayOf(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

// Check for leap year if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))) { daysInMonth[2] = 29 }

// Determine the starting day of the month val startDay = dayOfWeek(month, year)

// Print the calendar header println(" ${months[month]} $year") println("---------------------------------------") println(" Sun Mon Tue Wed Thr Fri Sat")

// Print the blank spaces before the starting day for (i in 0 until startDay) { print(" ") } fun Double.format(digits: Int) = "%.${digits}f".format(this) // Print the days of the month for (i in 1..daysInMonth[month]) { println("${i.toString().padStart(4, ' ')}")

if ((i + startDay) % 7 == 0) { println() } } }

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

Understanding Oracle APEX 5 Application Development

Authors: Edward Sciore

2nd Edition

1484209893, 9781484209899