What we learned-August 29, 2017

Calculator:

Today, we reviewed in class how to create an operator on BlueJ.

In order to make it, we had to learn a couple of things such as how to get user input, doing something with that input using operations, and we got a little taste of conditional statements towards the end.

Scan Function:

The Scan function is used to to get user input. In order to use it, you have to import the scan function library. A library is kind of like pre-made bits of codes that you can use easily. In this case, you will need this at the very  top of your code: import.util.Scanner;

And to actually use the code, it goes something like this:

Scanner name = new Scanner(System.in);
dataType variableName = name.nextDataType();

Example:

Scanner magic = new Scanner(System.in);

double a = magic.nextDouble();

Operations:

The four basic operations are +, -, *, /

Concatenation:

This means to link something together, usually a string and something else. And the symbol is represented by a + sign.

For example, if you have:

String yourName=Michelle;

System.out.println(“My name is ” + yourName);

It’s going to print out: My name is Michelle

Type Casting:

When you have multiple data types in one line, they are all going to be converted into one kind of data type by the computer. It depends on what data types you have and what comes first.

Here are a few of the situations we discussed in class:

double a=3.5
int b=2

System.out.println(a + b); will print out 5.5

System.out.println(“The sum of the two numbers is: ” + a+b) will print out The sum of the two numbers is 3.52
-What happened was a+b got evaluated as a string
-To print “the sum of the two numbers is 5.5″ you have to put a brackets around a+b

System.out.println(a+b ” is the sum of your two numbers”); will print out 5.5 is the sum of your two numbers.

To summarise, the few basic rules we discussed in class include:

  • If you have a string first, everything else will be converted into strings. If you want the things after the string to remain as their original data type, you must put brackets around them.
  • If you have just numbers with different data types, the most precise data type will be used.
  • If you have an operation first, it will calculate the result, then it will print the string.

 

Leave a Reply