**Taking a step back to sanity on this one, sorry about the difficulty of lesson 11**

Topics Discussed: Using a loop to count something for us.

Source Code for Lesson 12:

import java.util.Scanner; class Jtutorial1 { public static void main(String args[]){ Scanner input = new Scanner(System.in); /* int total=0, countDown=0; double average=0; do{ System.out.println("Enter a number between 1 and 1000: "); countDown=input.nextInt(); }while(countDown < 1 || countDown > 1000); for(int i=1; i<= countDown; i++){ total += i; System.out.println(i); } average = total / (double)countDown; System.out.println("The total of all the numbers between 1 and "+countDown+ " is "+total+" and the average is: "+average); //(int)x, (long)x, * / */ String hello="Hello World"; System.out.println("The length of the string hello is: " +hello.length()+ " And contains the words / characters: "+hello ); }//end main }//end class

HomeWork:

/** See math operations in java here: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html* Each of these operators can be called like I called Math.pow (ex: Math.abs(variable) )** Problems that you should try to solve-- Pick one or more!* A: Allow the user to enter a telephone number, use an if statement to invalidate numbers that are too long / short in length* if the telephone number is within the right length, seperate the numbers appropriately into how they would appear in your* country of origin: Ex. 7818789000 would turn into 781-878-9000 in the USA. (This won't require much but mod / division)* Hint: Use long or double if you need to use a big number.** B: Quadratic formula solver: The quadratic equation made each of us a little scared when we first saw it in algebra 1, write* a program that conquers the beast that is the quadratic formula (Google it)* C: Write a program that will allow you to input a number to select an operation (output a menu, then allow the user to select one).* In that menu you should be allowing the user to choose a shape (circle, square, rectangle, etc). Once they select it, ask them to* input the dimensions of it (circle can be radius, diameter, circumference, rectangle can be length, width, perimeter or area.... etc etc.)* Once you have taken in a few points of input, calculate the missing pieces of information. Perhaps someone has a rectangle* where they have the area and the length, but not the width. Using java, figure out what the missing width is.** D: Extra credit for people that are antsy to get some after the last 2 assignments (grades posted this weekend):* write a program that determines the last digit of 3^n power (3^3 = 27, thus 7). Hint, you can use n%4 to help you on this one.*/

**Casting numbers**

Casting numbers is an important part of any programming language that has certain roles that numbers fall into. The basic idea is this: assume that you have two numbers that you would like to divide, let’s assume you have 12 items that you want to divide equally among 7 people, assuming that we were to type that problem into java as an integer, we would get the following:

12/7 = 1

We can clearly see that this is wrong, but when you think about it, it’s also right. An integer cannot possibly hold a decimal value, therefore it being > 1.5 does not mean it will be rounded or anything of the sort… everything after the decimal place is truncated and discarded. Thus 1.71 (12/7) becomes 1. The way to have this resolve as we would like is as follows:

12/7.0; // 1.71.....

Now there’s a lot of different ways that you can cast numbers like this, here is a non-complete list of ways that you can cast integers into doubles for a moment:

double a=0, b=0, c=0, d=0, e=0, f=0, g=0; a = 12/7; b = 12.0/7; c = 12/7.0; d = 12.0/7.0; e = (double)(12)/7; f = 12/(double)(7); g = (double)(12)/(double)(7); System.out.println("a = "+ a); System.out.println("b = "+ b); System.out.println("c = "+ c); System.out.println("d = "+ d); System.out.println("e = "+ e); System.out.println("f = "+ f); System.out.println("g = "+ g); The output of the above is as follows: Program output: a = 1.0 b = 1.7142857142857142 c = 1.7142857142857142 d = 1.7142857142857142 e = 1.7142857142857142 f = 1.7142857142857142 g = 1.7142857142857142

**Using a loop to increment a number**

Essentially, we’re taking a number defined outside the scope of the loop and incrementing it by the counter of the loop:

int total=0; for (int i=0; i<10; i++){ total += i; }

In the above example we increment total by using the contents of i each time through the loop. If we preserve i for use outside the loop we can easily find the average by dividing out total by the final value of i.