User input

User input

User input is like it sounds wher the user is able to input the information they want the program to use, this is done in diffrent ways depending on what platform you are using. For example codeHS uses things like

int i= readInt(“…”);

while eclipse uses things like

System.out.print(” . . .”)

int k = scannerName.nextInt();

The reason they are different is because eclipse can import the scanner class while, codeHS from what I know doesn’t have you import anything, and the reason they don’t import scanner is probably either because its a online platform, so it doesn’t connect to your computer the same as eclipse or because it causes issues like slowing down the program.

For more information on user input for specific platforms go to the links below:

Eclipse

CodeHS

While

While

A while statement or loop has three parts. The first one is the initialization of the variable tested in the condition, before the actual loop. The second part is testing the variable before the loop starts so if the condition is not true the the body is not executed and it is possible that it will not be executed at all. While the last part is change the variable which means that somewhere the variable being tested must change at some point in the lop so that it will to continue on endlessly and crash your program.

The format for a while loop is;

while(condition)

{

<body>

}

Like with other loops and conditional statements if there is only one statement you don’t need the braces. Also, don’t put a semicolon after the while condition because then it will be an empty statement so the statements that were suppose to be in the loop would no longer be there.

For loop

For loop

For loops are iterations that will only ever go through the code inside the loop if the condition for the loop  is true(unlike do-while). There are four parts of a for loop. Those parts are setting a variable, a conditional statement(normally with the variable you set) and some change in the variable which is always an int at least from when I have used it and the code inside the loop (the code that is repeated).

A for loop is formatted as shown:

for(int i = somenumber; i  >somenum; i++)

{//if more than one line it needs brackets

<code in loop>

}

So the int doesn’t need to be there if you’ve declared i already and you want to declare i outside the for loop if you are going to use it at some point outside the for loop because otherwise it is out of its scope since the for loop counts as brackets, the conditional operator > can be replaced with any other conditional operator, and ++ can be replaced with anything like –, +2, -6, *2, /5(remember that it’s a int though) , or % 9.

Bank interest calculator

Bank interest calculator

The actual bank program is in the video below;

While the public or run method for this is below;

I got cut off at the end but i was going to say it works. To test the compound method just use amount=2, years =2, false for the answer to the simple or compound formula question and interest rate of .5 and it should equal 4.5 because 2*(1.5) = 3, and 3 * 1.5= 4.5, so the compound method also works.
Although the problem with this program is that it may not work very well with large numbers depending on what you are running it on so be careful about that.

Keep in mind there are always several of ways to do a program, the way I did it may not be the way you would’ve done it.

logic operators

logic operators

There are three logic operators and(&) or ( | | ) and not(!). If you read the post on gates you will see that logic operators follow the same rules. Those rules being that for and it takes two conditions and both must be true for the statement to be true, while with or only one statement must be true, for the entire thing to be true. Not is where if the condition/ comparison is true then the not turns it false, while if the condition/comparison is false it turns it true.

There are several smaller things involved with logic operators. Those things are short circuit evaluation which is where the compiler doesn’t go through the entire condition because it has already determined whether or not the entire condition will be false.

A more in depth explanation is at this link:(not posted yet)

Another thing is De Morgan’s laws or !(a && b) equals ! a || !q and where !(a || b) equals !a & !b.

Alse results of logic operators will be a boolean datatype, like gates and conditions generally are.

If Statements

If Statements

They are the more common conditional statement. They are more common since most of the time there are only a couple of possibilities, which makes a if-else statement a better fit for the program. A switch statement is normally used when there are several possibilities that may or may not branch off.

A if  statement is formatted:

if(condition)

{

}

Else if(condition)

{

}

Else

{

}

The only necessary part of a if statement is the if(condition) because else if is only used if there are 3 or more possibilities and else is only used if you need to do something if the condition on the if isn’t true. Also the braces are only necessary if there is more than one line for the statement( if, else if, or else).  

The condition is a comparison of some sort using either some sort of equals or campareTo method or comparison operators, and they can include logical operators in case either two or more things need to be true or the outcome is the same for multiple possibilities. For example say for a dice game if you role a 3 or a 7 you win. It doesn’t make sense to use if and else if since both of those possibilities will have the same outcome, so you only you the if and just make the condition (k=7 || k=3).

Also break statements can also be used in if statements , be careful with how you use the however because a break statement breaks out of the first set of braces and only the first set of braces so depending on whether or not the if statement has braces it could break out of the if statement or say a statement containing that if statement.

So a example for the break statements is:

if(condition)// this is the if statement that the break will get out of

{

. . .

if(condition)

Break;

. . .

}

if(condition)

{

. . .

if(condition)// this is the if statement that the break will get out of

}

Break;

}

. . .

}

A good example of if statement being used is in the fc converter public method step-by-step video.

Fahrenheit to Celsius converter

Fahrenheit to Celsius converter

an example of the actual methods involved in conversion:

an example of the run method or the public main void method to go along with the previous methods shown is down below:

There are some mistakes in the code so I will either redo these or make a video debugging them.

*Actually as far as I can tell the only issue is tytemp being the same as what you gave the program so, for it to work properly just split and move it into the if and else and put the proper notation. So like this:

if(tytemp.substring(0,2).equals(“fa”))
{
test.setFahren(num);
n = test.getCel();
System.out.print(n + “is the temperture in Celsius”);
}
else
{
test.setCel(num);
n = test.getFahren();
System.out.print(n + “is the temperture in fahrenheight”);
}

Conditional statements

Conditional statements

There are two main types of conditional statements in Java they are the If-else statements and the switch statements. A conditional statement essentially tests whether a expression which can have things like and(&) or(||) and not(!) as well as comparison operators(greater than, less than, equal to, ect) and things like the string method equals() .

If-else statement

switch statement:(post not ready yet)

Primitive datatypes

Primitive datatypes

Primitive types refers to some datatypes. Those datatypes are  boolean, char, int, double, float, long, byte, and short. These datatypes do not hold the properties of objects, like the Class type String does.Class type refers to datatypes that do hold the properties of objects.

Char is the datatype for unicode  and holds one character.

Int,double,float,long,short, and byte all refer to numbers.

Boolean is the datatype that’s value can only be true or false.