[go: up one dir, main page]

0% found this document useful (0 votes)
14 views10 pages

System Development (HSYD100-1) FA3

The document outlines a practical exercise for Systems Development 1, focusing on Java programming related to car sales data. It includes code snippets for creating arrays, generating random sales values, sorting integers, and finding the highest and lowest sales values for each month. The exercise emphasizes using arrays, loops, and object-oriented programming principles in Java.

Uploaded by

cadillaccylee111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

System Development (HSYD100-1) FA3

The document outlines a practical exercise for Systems Development 1, focusing on Java programming related to car sales data. It includes code snippets for creating arrays, generating random sales values, sorting integers, and finding the highest and lowest sales values for each month. The exercise emphasizes using arrays, loops, and object-oriented programming principles in Java.

Uploaded by

cadillaccylee111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Academic Year 2023: January – June

Formative Assessment 3: Systems Development 1 (HSYD100-1)


NQF Level, Credits: 5, 12
Assessment Type: Practical Exercise
Student NO: 39HA2307213
Name & Surname: Nkateko Chauke
Educator: P. Jideani

Due Date: 29 May 2023


Total: 50 Marks

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];

// Random values intialization


Random random = new Random();
for (int i = 0; i < carSalesValues.length; i++)
{
CarSalesValues[i] =
Random.nextInt(10000); // Generates a random number between 0 and 9999
}
```

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);

// Display the second array in a JOptionPane dialog box


JOptionPane.showMessageDialog(null,arrayAsString);
```
This code first retrieves the second array from `myArray` and stores it in a new
`String` array called `secondArray`. Then, it converts `secondArray` to a string
using the `Arrays.toString()` method, which returns a string representation of the
array's contents. Finally, the code displays the second array as a message in a
`JOptionPane` dialog box using the `JOptionPane.showMessageDialog()` method.

QUESTION 2
```java
import java.util.Scanner;

public class IntegerArraySorter {public static void main(String[] args) {int[]


integers = getIntegers(5);
System.out.printIn("Original array:"); printArray(integers);
sortIntegers(integers); System.out.printIn("Sorted array in decending order:");
printArray(integers) }

public static int[] getIntegers(int size) {Scanner scanner = new


Scanner(System.in; int[] integers = new int[size]; System.out.printIn("Enter" +
size + "integer:"); for (int i = 0; i < size; i++) {integers[i] = scanner.nextInt();} return
integers;
}

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;
}
}
}
}
}
```

Here's how the program works:


1. The `getIntegers` method prompts the user to enter integers and stores
them in an array of the specified size.
2. The `printArray` method prints each element of the array with its index.
3. The `sortIntegers` method uses a simple bubble sort algorithm to sort the
array in decending order.
4. In the `main` method, we call `getIntegers` to get the array of integers from
the user, then print the original array using `printArray`. We then call
`sortIntegers` to sort the array, and finally print the sorted array using
`printArray`.

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
}

public MonthlyCarSales(String mth, double val) {


saleMonth = mth;
saleValue = val;
}

// Setters
public void setSaleMonth(String nm) {
saleMonth = nm;
}

public void getSaleValue(double val) {


saleValue = val;
}

// Getters
public String getSaleMonth() {
return saleMonth;
}

public double getSaleValue() {


return saleValue;
}

// 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;

public class Main {

public static void main(String[] args) {

MonthlyCarSales[] salesArray = new MonthlyCarSales[12];


Random rand = new Random();
for (int i = 0; i < salesArray.length; i++) {
String month = "Month " + (i + 1);
double value = rand.nextDouble() * (500 – 2) + 2;
SalesArray[i] = new MonthlyCarSales(month, value);
}

// Print the sales values for each month


for (MonthlyCarSales sale : salesArray) {
System.out.printIn(sale);
}
}
}
```

In this example, we first create an array of 12 `MonthlyCarSales` objects called


`salesArray`. We then create a `Random` object called `rand` to generate the
random sales values. We then use a `for` loop to iterate over the `salesArray` and
generate a random sales value for each object using the `nextDouble()` method of
the `Random` class.
We use the formula `rand.nextDouble() * (500 – 2) + 2` to generate a random
double between 2 and 500. Finally, we print out the sales values for each month
using a `for-each` loop and the `toString()` method of the `MonthlyCarSales`
class.

3.3
```
import java.util.Random;
imoprt javax.swing.JOptionPane;

public class Main {


public static void main(String[] args) {
MonthlyCarSales[] salesArray = new MonthlyCarSales[12];
Random rand = new Random();

for (int i = 0; < salesArray.length; i++) {


String month = "Month " + (i + 1);
double value = rand.nextDouble() * (500 – 2) + 2;
salesArray[i] = new MonthlyCarSales(month, value);
}

// Find the highest and lowest sales value


double highestValue = 0;
String highestMonth = "";
double lowestValue = Double.MAX_VALUE;
String lowestMonth = "";

for (MonthlyCarSales sale : salesArray) {


double value = sale getSaleValue();
String month = sale getSaleMonth();
if (value > highestValue) {
highestValue = value;
highestMonth = month
}
if (value < lowestValue) {
lowestValue = value;
lowestMonth = month;
}
}

// Display the results using JOptionPane


String output = "Highest sales value: " + highestValue + " in " +
highestMonth + "\n" +
"Lowest sales value: " + lowestValue + " in " +
lowestMonth;
JOptionPane.showMessageDialog(null, output);
}
}
```

In this example, we first generate an array of 12 `MonthlyCarSales` objects and


populate it with random sales values. We then use a `for-each` loop to iterate over
the `salesArray` and find the highest and lowest sales values along with their
respective months.
We use the `getSaleValue()` and `getSaleMonth()` methods of the
`MonthlyCarSales` class to retrieve the values and months for each object and
compare them to the current highest and lowest values.
Finally, we use the `JOptionPane` class to display the results in a dialog box. We
create a string called `output` that contains the highest and lowest sales along
with their respective months, and pass it to the `showMessageDialog()` message
of the `JOptionPane` class.

You might also like