Class Notes (1,036,680)
CA (594,163)
(10)
Dancy (10)
Lecture 2

PROG 10082 Lecture Notes - Lecture 2: Scientific Notation, Equals SignPremium

4 pages128 viewsWinter 2016

Department
Computer Engineering Technology
Course Code
PROG 10082
Professor
Dancy
Lecture
2

Page:
of 4
Java Strings Lecture
the first thing when a program is running would be "Please enter the first word"
ie hello
Mistakes in java code
-basically, pay attention to syntax and ensure consistency. Follow the rules for proper
syntax, make sure there's enough brackets
-a data type explains what kind of variable is represented
-to convert a number to scientific notation it's powers of ten
-123.456 is (10^2)*1.23 for example
-you need to get the number to one sigfig for scientific notation
-to write the above in exponential notation, replace the 10 with an e as 1.23456e2
-a float is half as precise as double
-you would represent a byte, in machine language, with an integer in java when you are
using a compiler
-strings must be in double quotations
-char uses one single quotation mark like ' '
-/n is newline
-a char literal can only have one character
Source code from class:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
import.java.util.Scanner;
// this class takes in 3 words from the user and prints them out in reverse order
public class ReverseWords
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter the first word");
String first=sc.nextLine();
System.out.println("Please enter the second word");
String first=sc.nextLine();
System.out.println("Please enter the third word");
String first=sc.nextLine();
System.out.println(third);
System.out.println(second);
System.out.println(first);
System.out.print(first);
System.out.print(second);
System.out.print(third);
System.out.println();
//can also write as Systemout.println(third+ " " + second + " " + first);
//print out variables in reverse order
}
}
In class exercise notes:
public static void main(String [] args)
{
int first, second, third;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
first = in.nextInt();
second = in.nextInt();
third = first + second;
System.out.println("Sum of entered integers = "+ third);
}
}
import java.util.Scanner;
public classSquareNumber
public static void main(String [] args)
{
int first, second;
System.out.println("Enter a number to square it ");
Scanner in = new Scanner(System.in);
first = in.nextInt();
second = first * first;
System.out.println("Square of number = "+ second);
//another way to do a square of a number is to do int first and then int
square=Math.pow(first, 2)
}
}
we use null to represent zero
public class TestBank
{
public static void main(String [] args)
{
System.out.println(1.10-0.50);
}
}
we use big decimal for currency to keep precision
we use variables to show that these things change in value
the equals sign is the assignment operator
if you declare number=5, then the result is 25 for example
string cannot be converted to an int
camel case is lower case
the remainder is also called the modulus
-23%5 is -3 because -3 is the remainder of this result
3%-9 is 3 because 3 is left over, as -9 goes into 3 zero times
Source code to write if a number is odd or even:
public static void main(String [] args)
{
int first, second;
System.out.println("Enter a number ");
Scanner in = new Scanner(System.in);
first = in.nextInt();
second = first%2;
if ( (second) == 0 )
{
{even}
System.out.println("This is an even number");
else
{
{ odd}
System.out.println("This is an odd number");
System.out.println("The remainder when %2 is " + second);
System.out.println("If the remainder is 0, your number is even. Otherwise it is odd.");

Loved by over 2.2 million students

Over 90% improved by at least one letter grade.