0% found this document useful (0 votes)
5 views5 pages

HSYD100-1 Formative Assesment 3

The document contains Java code for a formative assessment by a student named Jason Scholtz, focusing on systems development. It includes examples of string comparison, user input validation, and a class for gym equipment with sorting functionality. The code demonstrates basic programming concepts such as conditionals, classes, and methods in Java.

Uploaded by

Jason Scholtz
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)
5 views5 pages

HSYD100-1 Formative Assesment 3

The document contains Java code for a formative assessment by a student named Jason Scholtz, focusing on systems development. It includes examples of string comparison, user input validation, and a class for gym equipment with sorting functionality. The code demonstrates basic programming concepts such as conditionals, classes, and methods in Java.

Uploaded by

Jason Scholtz
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/ 5

Jason Scholtz

ID:0211105136082

Student number: 40HA2500678

Email: [email protected]

Systems Development(HSYD100-1)

Formative Assessment 3
Question 1

1.1

package com.mycompany.javaassignment;

/**
*
* @author mrjas
*/
public class JavaAssignment {

public static void main(String[] args) {

// Step 1: Declare your variables


String shake1 ="VanillaShake";
String shake2 ="VANILLAshake";

//Step2:Use the if else statement


if(shake1.equalsIgnoreCase(shake2)){
System.out.println("Same product");
}else{
System.out.println("Different product");
}
}
}

1.2

package com.mycompany.javaassignment;
import java.util.Scanner; // We are importing this scanner to get the users input
/**
*
* @author mrjas
Public class DietTracker {
Public static void main(String[] args) {
//Create a Scanner Object to read input from the user
Scanner scanner = new Scanner(System.in);

// we ask the user to put in a character


System.out.print(“Enter a Charater(letttr for food name or digit for calorie) : “);
char ch = scanner.next().charAt(0);//Read the first character of the input

//Use character class method to check the type of character


if(Character.isLetter(ch){
System.out.println(“Valid input: It is a letter.”);
}else if(Character.isDigit(ch)){
System.out.println(“Valid input: It is a digit.”);
}else{
System.out.println(“Invalid input: The character is neither a letter or a digit.”);
}
//Close the scanner to avoid memory leaks
Scanner.close();
}
}

Question 2

2.1

package com.mycompany.javaassignment;

/**
*
* @author mrjas
*/
public class FitnessStore {

// Inner class for equipment


static class GymEquipment {
String name;
int price;
String category;

public GymEquipment(String name, int price, String category) {


this.name = name;
this.price = price;
this.category = category;
}

public void displayDetails() {


System.out.println(category + " - " + name + " - R" + price);
}
}

public static void main(String[] args) {


// Array of beFit (GymEquipment) objects
GymEquipment[] equipmentList = new GymEquipment[4];
equipmentList[0] = new GymEquipment("Dumbbells", 300, "Strength Training");
equipmentList[1] = new GymEquipment("Treadmill", 7000, "Cardio");
equipmentList[2] = new GymEquipment("Yoga Mat", 150, "Flexibility");
equipmentList[3] = new GymEquipment("Resistance Bands", 200, "Endurance");

// Loop to display details


for (int i = 0; i < equipmentList.length; i++) {
equipmentList[i].displayDetails();
}
}
}

2.2

public class FitnessStore {

// Inner class for equipment


static class GymEquipment {
String name;
String price; // Price is stored as a string
String category;

public GymEquipment(String name, String price, String category) {


this.name = name;
this.price = price;
this.category = category;
}

public void displayDetails() {


// Displaying category, name, and price
System.out.println(category + " - " + name + " - R" + price);
}
}

// Method to bubble sort by price


public static void bubbleSortByPrice(GymEquipment[] equipmentList) {
for (int i = 0; i < equipmentList.length - 1; i++) {
for (int j = 0; j < equipmentList.length - 1 - i; j++) {
int price1 = Integer.parseInt(equipmentList[j].price); // Convert price to
integer
int price2 = Integer.parseInt(equipmentList[j + 1].price); // Convert price to
integer
if (price1 > price2) {
// Swap the elements if they are in the wrong order
GymEquipment temp = equipmentList[j];
equipmentList[j] = equipmentList[j + 1];
equipmentList[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {


// Array of GymEquipment objects
GymEquipment[] equipmentList = new GymEquipment[4];
equipmentList[0] = new GymEquipment("Dumbbells", "300", "Strength Training");
equipmentList[1] = new GymEquipment("Treadmill", "7000", "Cardio");
equipmentList[2] = new GymEquipment("Yoga Mat", "150", "Flexibility");
equipmentList[3] = new GymEquipment("Resistance Bands", "200", "Endurance");

// Sort equipment list by price


bubbleSortByPrice(equipmentList);

// Use a for-each loop to display sorted equipment details


for (GymEquipment equipment : equipmentList) {
equipment.displayDetails();
}
}
}

You might also like