java

Cards (17)

  • Declaring variables

    Int wholeNumber = 0;
    double realNumber = 0.0;
    String words = " ";
    char charcter = 'a';
  • Get info from user
    System.out.println("question to display goes here");
    VariableName = keyboard.nextInt(); keyboard.nextLine()
  • Declaring Arrays
    Int [] arrayWholeNumbers = new int [10];

    An empty array of 10 integers called arrayWholeNumbers

    Double [] arrayRealNumbers = new double [5];

    An array called arrayRealNumbers containing 5 empty doubles

    String [] array words = new string[20];

    An array of 20 empty strings called arrayWords
  • Pre defined arrays declaring
    Int[] listIntNumbers = {7, 5, 4, 3, 2, 1};
    An array of int called listIntNumbers starting with 7,5,4,3,2,1

    Double [] listRealNums = {0.1, 2.2, 4.5, 3.3};
    An array called listRealNumbers containing 0.1, 2.2, 4.5, 3.3

    String [] listWords = {"hello", "goodbye", "welcome"};
    The values hello goodby welcome stored in an array of strings called listWords
  • Assignment and calculations
    Whole number = 0;
    Whole number = 0

    Real number = wholenumber /10;
    Real number number equals wholenumber divided by 10

    Perimeter = 2*(length + breadth);
  • Data output to user

    System.out.println("the message you want to be displayed");
  • Adding a variable into an output to user
    System.out-printin("The + sign is concatenation in Strings" +
    words + " you can add numbers in as well" + wholeNumber);

    String outputSentance = "";
    outputSentance = words + "you can Join together strings like this \n";

    The "/n" adds a new line to the output

    The outputsentance = output + "new text" concatenates the new text on to the
    ned of output
  • Logical operators
    (wholeNumber > realNumber)
    (wholeNumber < realNumber)
    (wholeNumber <= realNumber)
    (wholeNumber >= realNumber)
    (wholeNumber == realNumber) (wholeNumber |= realNumber)
    (words. equals ("Hello" ))
    (I (words. equals ( "Hello")))
    ( (words. equals("Hello" )) && (wholeNumber ›= realNumber))
    ( (words equals ("Bello")) ||
    (wholeNumber >= realNumber)
  • Selection simple
    If (mark > 50) {
    System.out.println("you have passed");
    }


    If mark is greater than 50 then display you passes end if
  • Selection with else
    If (mark > 50) {
    Output = "well done you passed ";
    } else {
    Output = "you have failed";
    }
    System.out.println(output);



    If mark is greater that 50 then
    Display you passed on screen
    Else
    Display you have failed
    End if
  • Selection nested
    if (mark > 70) {
    output = "You got an A in the test":
    } else if (mark > 50) {
    output = "Well done you passed";
    } else {
    output = "You have failed";
    }
    System. out. printin(output);
  • Fixed loop
    int total = 0;
    for (int index = 0; index < 10; index ++){
    System.out.println("Please enter the score");
    int score = keyboard.nextInt(); keyboard.nextLine();
    total = total + score;
    }



    Set integer total to 0
    Repeat 10 times
    Get integer score from user using keyboard
    Add score to total
    End loop
  • Conditional loop
    System.out.printin("please enter the score between 0 and 10");
    double score = keyboard.nextDouble()n keyboard.nextLine();
    while (score < 0 || score > 10){
    System.out.printin("Error: Please enter a score between 0 and 10");
    score = keyboard.nextDouble(); keyboard.nextLine();
    }

    Get real value from keyboard as score
    While score < 0 or score > 10 loop
    Display error must be between 0 and 10
    Get real value from keyboard as score
    End loop
  • Random numbers
    Int random = N5.randomInt(10);

    Set int random number to a random number between 0 and 10
  • Rounding numbers
    AverageAge = N5.roundDp(averageAge, 2);

    Round averageAge to two decimal places


    Int averageAgeint = N5.roundToInt(averageAge);

    Used to get rid of the .0 at the end of number if required
  • Length of string
    String name = "Mr Hendry";
    If (name.length() > 10) {
    System.out.println("that is a long name");
    }


    If length of name is more than 10 characters display wow that is a long name
    End if
  • Working with arrays
    String [] names = new string [10];
    For (int index =0; index < names.length; index ++){
    System.out.println(" please enter the persons name");
    Names [index]= keyboard.bextline();
    }



    Create an empty array of number of people strings called names

    Repeat number of people 10 times

    Get value for for current index of array names from keyboard

    End loop