System Development (HSYD100-1) FA3
System Development (HSYD100-1) FA3
QUESTION 1
(a)
```
Int[] carSalesValues = new int[12];
```
This creates an integer array named `carSalesValues` with a size of 12, which
corresponds to the 12 months of the year. You can then use each element of the
array to store the total car sales value for a specific month, where
`carSalesValues[0]` represents January, `carSalesValues[1]` represents February,
and so on.
(b)
```
String[] monthNames = {''January'', ''February'', ''March'', ''April'', ''May'', ''June'',
''July'', ''August'', ''September'', "October", ''November'',
''December''};
```
This creates a string array named `monthNames` and initializes it with the names
of the months of the year in order. You can then use each element of the array to
retrieve the name of a specific month, where `monthNames[0]` represents
January, `monthName[1]` represents February, and so on.
(c)
```
Import java.util.Random;
// Array declaration
Int[] carSalesValues = new int[12];
And here's how you can determine and display the highest sales value and the
month of which it occurred using the arrays `carSalesValues` and `monthNames`:
```
Import javax.swing.JOptionPane;
// Find the maximum sales value and its index
int maxSales = carSalesValues[0]; int maxIndex = 0;
for (int i = 1; i < carSalesValues.length; i++)
{
if (carSalesValues[i] > maxSales) {
maxSales = carSalesValues[i]; maxIndex = i;
}
}
// Display the output using JOptionPane String message = "The highest sales
value occurred in " + monthNames[maxIndex] + "with a value of R" + maxSales;
JOptionPane.showMessageDialog(null, message);
```
This code first finds the maximum sales value and its index position in the
`carSalesValue` array using a loop. It then uses the `maxIndex` variable to retrieve
the corresponding month name from the `monthNames` array. Finally, it displays
the output using `JOptionPane`.
(d)
```
// Array declaration String[] jMonths = new String[4]; // Maximum number of
elements is 3 (June, July, January)
// Loop to check each month name in monthNames array int jCount = 0; // Counter
for number of elements in jMonths array for (int i = 0; i < monthNames.length; i++)
{if monthNames[i].startsWith(“J”)) {jMonths[jCount] = monthNames[i]; jCount++;
}
}
// Print the jMonths array System.out.printIn(“The months starting with ‘J’ are:”);
for (int i = 0; i < jCount; i++) {System.out.printIn(jMonths[i]);
}
```
This code declares another string array named `jMonths` with a maximum size of
3, which corresponds to the maximum number of months that start with the letter
‘J’ (June, July, and January). It then loops through each element of the
`monthNames` array and checks if the month name starts with the letter ‘J’. If it
does, it assigns the month name to the `jMonths` array and increments the
`jCount` counter. Finally, it prints the `jMonths` array to the console.
(e)
```java
// Get the second array to a string using Arrays.toString()
String arrayAsString = Arrays.toString(secondArray);
QUESTION 2
```java
import java.util.Scanner;
public static void printArray(int[] array) {for (int i = 0; i < array.length; i++)
{System.out.printIn("Element " + i " = " + array[i]);}
}
public static void sortIntegers(int[] array)
{
for (int i = 0; i < array.length - 1; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] < array[j]) {
int temp = array[j];
array[i] = array[j];
array[j] = temp;
}
}
}
}
}
```
Note that this program assumes that the user will enter exactly 5 integers. You
could modify the `getIntegers` method to prompt the user for the size of the array
and dynamically allocate the array based on the user's input if you wanted to
make the program more flexible.
QUESTION 3
3.1
```
public class MonthlyCarSales {
// Attributes
private String saleMonth;
private double saleValue;
// Constructors
public MonthlyCarSales() {
// Default constructor
}
// Setters
public void setSaleMonth(String nm) {
saleMonth = nm;
}
// Getters
public String getSaleMonth() {
return saleMonth;
}
// Other methods
public String toString() {
return "Month: " + saleMonth + ", Value: " + saleValue;
}
}
```
This class has two attributes, `saleMonth` and `saleValue`, which represent
the month and the value of the car sales for that month. It also has two
constructors, one of which is a default constructor that sets both attributes
to null or 0, and another that takes in arguments for the month and value of
sales. The class also has setter and getter methods for each attribute, as
well as a `toString()` method that returns a string representation of the
object.
3.2
```
import java.util.Random;
3.3
```
import java.util.Random;
imoprt javax.swing.JOptionPane;