CSE20 : Lab #4 - Data Types

Similar documents
COMP String and Console I/O. Yi Hong May 18, 2015

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Getting started with Java

COMP Introduction to Programming Primitive Types, Strings and Console I/O

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Programming with Java

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

First Java Program - Output to the Screen

Elementary Programming

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Full file at

Lecture Set 2: Starting Java

We now start exploring some key elements of the Java programming language and ways of performing I/O

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Lecture Set 2: Starting Java

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Fundamentals of Programming Data Types & Methods

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Introduction to Java Applications

A variable is a name for a location in memory A variable must be declared

Full file at

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CIS 110: Introduction to Computer Programming

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Object Oriented Programming. Java-Lecture 1

CS111: PROGRAMMING LANGUAGE II

Peer Instruction 1. Elementary Programming

Basic Computation. Chapter 2

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Reading Input from Text File

2 nd Week Lecture Notes

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS110: PROGRAMMING LANGUAGE I

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

CMPT 125: Lecture 3 Data and Expressions

What did we talk about last time? Examples switch statements

CIS133J. Working with Numbers in Java

Program Fundamentals

COMP Primitive and Class Types. Yi Hong May 14, 2015

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Introduction to Computer Programming

CMSC131. Introduction to your Introduction to Java. Why Java?

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

2: Basics of Java Programming

download instant at

Building Java Programs

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Introduction to Software Development (ISD) David Weston and Igor Razgon

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

I. Variables and Data Type week 3

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

CS 200 Using Objects. Jim Williams, PhD

Chapter. Let's explore some other fundamental programming concepts

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Basic Computation. Chapter 2

Computational Expression

AP Computer Science A

Building Java Programs

Arithmetic and IO. 25 August 2017

CS 106 Introduction to Computer Science I

Assignment 2.4: Loops

Introduction to Java Applications; Input/Output and Operators

Using System.out.println()

Chapter 2: Data and Expressions

Java Bytecode (binary file)

Object-Oriented Programming

Section 2: Introduction to Java. Historical note

Chapter 2: Using Data

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CS 302: Introduction to Programming

Programming Language Basics

ECE 122 Engineering Problem Solving with Java

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

1007 Imperative Programming Part II

COMP 202 Java in one week

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Chapter 2 ELEMENTARY PROGRAMMING

AP Computer Science Unit 1. Writing Programs Using BlueJ

CSE 142, Summer 2014

CHAPTER 4 MATHEMATICAL FUNCTIONS, CHARACTERS, STRINGS

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Lecture 1. Course Overview Types & Expressions

Building Java Programs

Objectives. Primitive Types, Strings, and Console I/O. Variables and Values. Outline. Naming and Declaring Variables. Variables and Values

Lab # 02. Basic Elements of C++ _ Part1

Welcome to the Using Objects lab!

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Welcome to the Primitives and Expressions Lab!

Transcription:

CSE20 : Lab #4 - Data Types Overview This week we will be exploring different data types and the different meanings we can extract from the same bits. It s a look inside the hood at what we covered in lecture and some types you have already used in previous lab. This lab is also aimed at making you more comfortable with everything we have done so far. Variables We relied heavily on variables in lab 3 after introducing them in lecture. It is important to understand the use of variables and difference in the errors or warnings that stems from their usage. Generally it is useful to give meaningful names to the variables so whoever is reading the code (including yourself) can quickly discern their purpose. So rather than using names like string1, you should also use names like hometown if you want it to store the person s hometown. Declaration It is an error to use a variable before declaring them since technically it does not exist yet. There are three ways of doing this from what you have already seen: int age; // Just declaring the type (int) and name (age) int age = 1; // Includes initialization of value 1 int age = input.nextint(); // Declare and assign value at the same time Once you declared a variable, it can be manipulated (write) or referenced (read) from then after. Write Assignments are done with the operator = so anytime you see a variable = means we are giving it a new value. We can see in the last two examples of declaration that assignment is done at the same time. Once a variable exists then you can always assign it any valid value for its type. If you never use a variable after declaring then it will create a warning to make sure you didn t mistype the name or forgot about it. In general, only create variables you will assign values and actually use. Read If you read a variable s value before any prior assignment then the value of the variable is undetermined. So it is prudent and good programming practice to always initialize before reading any variable. Whenever you refer to a variable then its value is read and used. If a variable is not read from then it will have a

warning saying you never used it. It is helpful in debugging in case the names are mismatched or you just simply forgot about the variable. So let s take a look at simple example from Averages.java: average = (n1 + n2)/2; Here we read the values of n1 and n2. We then add the two numbers and divide by 2. The resulting number is then written or assigned to the variable average. Then we read average inside a println statement like this: System.out.println(average); Notice that putting around anything makes it a string as the following illustrates: System.out.println( average ); This will always print out the words average and not the value inside the variable average. Notice that we didn t write our equation above as ( n1 + n2 )/2 which would be an error. So whenever we refer to the variable then it should just be by its name. String literals Now we talk a little more about strings which we used but have not discussed in depth. Everyone is familiar with the simple form of creating strings by putting quotes around it. Ever since the first lab we have figured out how to form sentences using strings. In lab 3, we created string objects and stored the input from the user. We can actually manipulate strings just like other objects or numbers. One common way use is to reduce the number of print statements we need to call. For example before we would use multiple lines to print the following sentence: System.out.print( Their age is ); System.out.print(age); System.out.println(. ); Just like numbers we can add strings together which is actually called concatenation. So we combine the above into: System.out.println( Their age is + age +. ); We can now use this form to create dynamic strings.

Type casting The meaning of a number depends on the type it represents. For char type 65 represents a character A. For int type it is just 65 and for float it will be 65.0. We can see the differences by running the following code: char chara = A ; System.out.println("OUTPUT is " + (char) (1 + chara)); System.out.println("OUTPUT is " + (short) (1 + chara)); System.out.println("OUTPUT is " + (int) (4/3)); System.out.println("OUTPUT is " + (float) (4/3)); System.out.println("OUTPUT is " + (double) (4/3)); System.out.println("OUTPUT is " + (float) 1); By putting a type like (int), we can force the number or expression to be that type. If we do (int) 4/3 then its forcing 4 to be an int before division by 3. If we use (int) (4/3) then the result of 4 divided 3 is being forced to type int. Using parentheses is very important to see what the type casting is being applied to. Play with this in your lab to see the different effects before you attempt the exercises. Use of Scanners We need to add one line for Scanner objects in order to handle multiple word strings so the answers can be more complete. Also nextline has a funny behavior when combined with other types due to how newlines are handled. So along with the declaration of a Scanner variable, we set it to use newlines to separate inputs instead of any whitespace (like space or tab). Scanner input = new Scanner(System.in); input.usedelimiter(system.getproperty("line.separator")); // add age = input.nextint(); city = input.next(); Please note that we only need one Scanner to read all the user inputs in a given program. You just call next* methods as you have before to get the different inputs. (Brainstorm) Review What does it mean to be case-sensitive? Does a program behave in the same way every time you run it? Give the expression for printing OUTPUT is Z using variable chara by using type casting and manipulating it (like in the examples)

Getting started After starting Eclipse, create a new project called Lab 4. You can also import Interviewer.java file from Lab 3 if you wish to do the second part of this lab. You may want to look at Averages.java from Lab 3 as an example for asking user for two numbers as input and then outputting a result after calculating it. (Exercise) Manipulating Numbers You need to understand Type Casting before you do this exercise. Create a new class called Manipulate with the following behavior Ask for 2 int numbers from the user just like Averages.java. Then you will display 4 results using those two numbers: Add of n1 + n2 is **** Sub of n1 n2 is **** Mul of n1 * n2 is **** Div of n1 / n2 is **** Obviously instead of **** it ll be the result of the operation. Be sure that the result is an int by casting it before printing it out. Ask for additional 3 types of data from user o 2 shorts o 2 floats o 2 doubles For each of the 3 types perform all 4 math operations where the result should match the original type, ie addition of two shorts results in a short o +, -, *, / For each operation, print out the results like you did for ints Make sure proper types are created for each output There should be a total of 16 output lines. (Assessment) Level of understanding Are there certain numbers that do not work for input of Manipulate? Are there any output differences between floats and doubles? Can two large integers after some operation result in a short?

(Exercise) Interviewer Program Modify the Interviewer from lab 3 with the following requirements Answers must be able to handle sentences or numbers Add a question about weight in lbs (int type) 1 kg = 2.2 lbs Output the user s weight in kgs (int type) Ask all the questions first before the results are printed out Output is all in one paragraph like Biography of the interviewee What to hand in When you are done with this lab assignment, you are ready to submit your work. Make sure you have done the following before you press Submit: Include answers to Brainstorm and Assessment questions Attach your Manipulate.java Attach your new Interviewer.java List of Collaborators