Skip to content

Performance Test (Intermediate Level)

Henry Hare edited this page Jun 7, 2023 · 9 revisions

Error Catching Task

The code below is designed to print all the numbers from the list testList There are ways to optimize it too if you find them, however that is not required.

Python

index = 0 testList = [2,5,7,4,11]

while index < 6: print(testList[index]) index += 1

Java

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

Integer index = 0;
Integer[] testList = {2,5,7,4,11};

while (index < 6) {
  System.out.println(testList[index]);
  index += 1;
}

} }

Optimize Task

Python

minecraftCost = 24.99 tShirtCost = 18.99 raspberryPiCost = 999.99 tax = 1.1

print("Hello user!") displayText = "The cost of this item is " + str(minecraftCost) print(displayText) taxCost = minecraftCost * 1.1 print("With tax, this item will cost " + str(taxCost))

print("Hello user!") displayText = "The cost of this item is " + str(tShirtCost) print(displayText) taxCost = tShirtCost * 1.1 print("With tax, this item will cost " + str(taxCost))

print("Hello user!") displayText = "The cost of this item is " + str(raspberryPiCost) print(displayText) taxCost = raspberryPiCost * 1.1 print("With tax, this item will cost " + str(taxCost))

Java

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

  Double minecraftCost = 24.99;
  Double tShirtCost = 18.99;
  Double raspberryPiCost = 999.99;
  Double tax = 1.1;

  System.out.println("Hello User!");
  String originalCost = "The cost of this item is: " + minecraftCost * tax;
  System.out.println(minecraftCost);
  Double taxCost = minecraftCost * tax;
  System.out.println("With tax, this item costs: " + taxCost);

  System.out.println("Hello User!");
  originalCost = "The cost of this item is: " + tShirtCost * tax;
  System.out.println(originalCost);
  taxCost = tShirtCost * tax;
  System.out.println("With tax, this item costs: " + taxCost);

  System.out.println("Hello User!");
  originalCost = "The cost of this item is: " + raspberryPiCost * tax;
  System.out.println(originalCost);
  taxCost = raspberryPiCost * tax;
  System.out.println("With tax, this item costs: " + taxCost);
  }
}

Create Task

Create a program that...

  • Will ask the user for there name and age repeatedly
  • uses an if statement to stop the loop
  • once the loop is over, print out all the names and there ages

Python

in order to get input from the user, you can use the method below input() an example can be seen below userInput = input("What is your eye color")

As seen from the example, you can create a variable, and store the user input in it You may have also noticed that there is a string as a parameter in the input method. This string will be printed to the terminal whenever the mehtod is run

Java

In order to get input from the user, first you must import the scanner method import java.util.Scanner Then, you must create the Scanner Object Scanner scanObject = new Scanner(System.in); // Create Scanner object in order to get input, you must use this method of the the Scanner object (scanObject) String userInput = scanObject.nextLine();

an example of this in full swing can be seen below

import java.util.Scanner;

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

Scanner scanObject = new Scanner(System.in); // Create Scanner object
System.out.println("Enter your social security number!");

String SSN = scanObject.nextLine(); // get user input, store it on String "SSN"
System.out.println("Your Social Security Number is: " + SSN); // output 

} }

Clone this wiki locally