0% found this document useful (0 votes)
12 views5 pages

COSC1046 - Chapter 2

Uploaded by

shekhar bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

COSC1046 - Chapter 2

Uploaded by

shekhar bhandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

Chapter 2 – Elementary Programming

Numeric types and Variables:

There is also the char, which is the same size as a byte, and stores ASCII characters.

We use these data types with variables, and can assign their values like this:

int x = 10;

This creates a new space in memory that is the size of the int data type, and stores the value 10 in it.

You can also assign variable values to each other:

int x = 10;

int y = 5;

x = y;

It is important to note that x is now referencing the same place in memory as y

When naming your variables, you should make sure to name them something that makes sense, and is
easy to understand. Using variable names of x, y, or z should be avoided unless they make sense to use
in the situation.

Multi-word variables in Java are written in camelCase. This means that the first letter is always
lowercase, and the first letter of any other word is uppercase.

Ex.

double interestRate = 0.05;

int cardNumber = 123456789;


Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

Calculations with variables:

When assigned values to variables we can use calculations, such as:

int x = 3 + 4;

int y = 5*6;

int z = x + y;

You can have addition, subtraction, multiplication and division, exponents, and brackets. BEDMAS is
followed in all calculations

Exponents

Exponents can be calculated using the Math.pow() function:

int x = Math.pow(base, power)

int x = Math.pow(2, 3) //x = 8

Another if you want to adjust a variable by a certain amount you can use the following:

Augmented operator Equivalent


X += 5 X = x +5
X -= 5 X=x–5
X *= 5 X=x*5
X /= 5 X=x/5

These are not necessary, but it can make code simpler and easier to read.

If incrementing or decrementing a variable by 1, you can use ++ or - - to do that

X++ //increments x by one

x-- //decrements x by one

Reading input from the console:

To read input from the user, we’ll be using an object that is called a Scanner. We need to import it
before we can use it. Type:

import java.util.Scanner;

Above your class so that you can access the Scanner object.

Creating a Scanner:

To create a Scanner you’ll write:


Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

Scanner input = new Scanner(System.in);

Input is the name that we’ve given our new Scanner, and we’ve assigned a new Scanner object to it-
you’ll learn more about objects later in the course, for now, just understand that’s how you make a
Scanner. The System.in part tells it we’re getting input from the console.

Now try writing in your main method: int number = input.nextInt();

And after that code write System.out.println(“You’ve entered “ + number);

What the nextInt method that Scanners have does it wait until the user enters an input and accepts it as
an integer. Run your code and type a number in the console, press enter once you’re finished

What happens if you don’t give it an integer? What kind of error would that be?

Most of the time when getting input this way, we will print out a prompt before getting the input from
the user. Try:

System.out.print(“What is your favorite number? “);

int favoriteNumber = input.nextInt();

System.out.println(“Your favorite number is: “ + favoriteNumber);

Notice how we used System.out.print instead of System.out.println. This is because it is easier for the
user to understand what they are typing their input for if it’s on the same line as the prompt.
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

Assignment Portion – hand in your work when completed

package welcometojava;

import java.util.Scanner;

public class String {

public class TripCostCalculator {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the distance to drive in miles: ");

double distance = input.nextDouble();

System.out.print("Enter the fuel efficiency of the car in miles per gallon: ");

double mpg = input.nextDouble();

System.out.print("Enter the price per gallon of fuel: ");

double pricePerGallon = input.nextDouble();

double gallonsNeeded = distance / mpg;

double cost = gallonsNeeded * pricePerGallon;

System.out.printf("The cost of the trip is $%.2f%n", cost);

}
Name: Vishw Patel Student I’d(229689040) COSC1046 Lab 1

You might also like