Homework Set 1- Fundamentals

Similar documents
AP Computer Science Homework Set 1 Fundamentals

AP Computer Science Homework Set 1 Fundamentals

AP Computer Science Homework Set 5 2D Arrays

AP Computer Science Homework Set 5 2D Arrays

AP Computer Science Homework Set 3 Class Methods

AP Computer Science Homework Set 2 Class Design

AP Computer Science Homework Set 2 Class Design

AP Computer Science A

QUIZ: What value is stored in a after this

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

Datatypes, Variables, and Operations

COMP 202 Java in one week

APCS Semester #1 Final Exam Practice Problems

Homework Set 2- Class Design

4. Inputting data or messages to a function is called passing data to the function.

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

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

BASIC ELEMENTS OF A COMPUTER PROGRAM

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 3: Operators, Expressions and Type Conversion

Basic Operations jgrasp debugger Writing Programs & Checkstyle

AP Computer Science Homework Set 3 Class Methods

CS 106 Introduction to Computer Science I

Building Java Programs

REVIEW. The C++ Programming Language. CS 151 Review #2

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

COMP 202 Java in one week

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Topic 4 Expressions and variables

Java Simple Data Types

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Chapter 4 Introduction to Control Statements

6.096 Introduction to C++ January (IAP) 2009

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

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

COMP 202. Java in one week

Building Java Programs

Course Outline. Introduction to java

Programming Basics. Digital Urban Visualization. People as Flows. ia

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

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Java Basics. SP17 ICS 111 Ed Meyer

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Visual C# Instructor s Manual Table of Contents

Building Java Programs

Introduction to Java Applications

COP 1220 Introduction to Programming in C++ Course Justification

CS152 Computer Architecture and Engineering VLIW, Vector, and Multithreaded Machines

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Introduction to Computer Science Unit 2. Notes

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

4 Programming Fundamentals. Introduction to Programming 1 1

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Total Test Questions: 43 Levels: Grades Units of Credit:.50

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

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

Object-Oriented Programming

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Key Java Simple Data Types

CIS 110 Introduction to Computer Programming. February 29, 2012 Midterm

JAVA OPERATORS GENERAL

Repetition Structures

Class #1. introduction, functions, variables, conditionals

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Chapter 2: Data and Expressions

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

AP CS Unit 3: Control Structures Notes

PRECALCULUS MR. MILLER

Enroll in MyCardStatement.com Client User Guide

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Full file at

Control Statements: Part 1

CSE 142, Summer 2015

CS 302: Introduction to Programming

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

c) And last but not least, there are javadoc comments. See Weiss.

Chapter 2: Data and Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

CSE 142, Summer 2014

SECTION II: LANGUAGE BASICS

Java Simple Data Types

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Introduction to Programming Using Java (98-388)

CS 231 Data Structures and Algorithms, Fall 2016

Simple Java Reference

Chapter 6 Primitive types

Midterm Review Session

Introduction to Computer Science Unit 2. Notes

Transcription:

1 Homework Set 1- Fundamentals Topics if statements with ints if-else statements with Strings if statements with multiple boolean statements for loops and arrays while loops String ".equals()" method "==" vs. ".equals() Arrays and for loops Arrays and random numbers By the end of the lesson students should be able to: a) Type in, compile, and run a Java program. b) State the basic Java program structure and components (source file, class, and methods). c) State and describe the two types of errors in programs: logical and syntactical. d) Debug a Java program with both logical and syntactical errors. e) Write while and for loops that are syntactically correct. f) Write if and if-else statements that are syntactically correct. g) Use the relational operators >, <, >=, <=, = =, and!= within the context of an if statement, for loop, and while loop. h) Use the Boolean operators && and within the context of if statements and while loops. i) Write comments using the // and /* */, and javadoc constructs. j) Write the Java code to create a String object. k) Write the Java code to create an array of String objects or any primitive type. l) Write the Java code to create an ArrayList of String objects. m) Write the Java code to access any element in an array using [] notation. n) Write the Java code to print output using System.out.println(). o) Write code using Math.random() that returns a floating point number (double) from 0 inclusive to 1 exclusive. p) Write the Java code to create random numbers in any given range using Math.random().

P1A P1A. Using FirstProject.java as a model, write a similar program, MySecondApp.java, that prints your favorites. Your program should print your food, your favorite color, your favorite movie, and your favorite musical group each on a separate line. Example: 2

P1B P1B. Now let s practice using an if statement and the <, >, and == operators. Write a program that creates two int variables called a and b and assigns them each an integer values. The program should print a message (using System.out.println) telling the user which if a < b, if a > b, or if a is = to b. Here s some sample output if a holds the value of 10 and b holds the value of 15. The value of a is 10 and it is less than b whose value is 15. Here s some sample output if a holds the value of 20 and b holds the value of 15. The value of a is 20 and it is greater than b whose value is 15. And if the value of a is equal to b, then print: The value of a is 10 and it is equal to b whose value is 10. When you are finished, change the values of a and b so you can check to see if your code works. Does it work with negative numbers? Why or why not? 3 P1C P1C. Write a program that uses a for and if-else statement that will print whether a number is an even OR an odd number. Here s some sample output if the number of 15 (or any odd number): 15 is an odd number And if the value is 8 (or any even number): 8 is an even number Note: you can use the modulus or remainder operator (%) to determine the remainder of a division. For example, if the following lines of code is executed, the value of remainder1 will be 1, since 2 goes into 15 seven times with a remainder of 1. However, the value of remainder2 will be 0 since 2 goes into 16 eight times with a remainder of 0. int remainder1 = 15 % 2; // remainder1 is 1 int remainder2 = 16 % 2; // remainder2 is 0 int remainder3 = 17 % 2; // remainder3 is 1 The modulus operator is helpful for finding remainders, but it is also the foundation for public-key cryptography which allows us to send information such as grades, online purchases, credit card payments, and bank statements securely through the internet. Extension: Write a program that will print whether a number is an even OR an odd number for numbers 1 through 50.

P1D P1D. Write a program that uses a for loop to print numbers 1 through 10 along with its square and a random number between the number and its square inclusive, each separated by a tab. In Java, you can create a tab by using the escape code \t as shown below: System.out.println( number + \t + numbersquared); 4

P1E P1E. Write a program that creates an array of Strings of your top three favorite movies and an array of Strings of your top three favorite songs. Then use a for loop to print out your three favorite movies on separate lines and a while loop to print out your three favorite songs all on the same line, separated by commas. Leave two blank lines between the movies and the songs. 5

P1F P1F. Write a program that creates an array that can hold 50 integer values. Using a for loop and Math.random(), fill the array with random integer values between 0 and 100 inclusive. Use a separate for loop to print each of the numbers in the array with a single space between each number. Note: When using a for loop with arrays, use the variable name index as the counter in the loop as shown below: for( int index = 0, index < myarray.length; index++ ) 6

P1G P1G. Write a program that creates an array that can hold 10 integer values. Then fill and print the array according the following specifications: a) Use a for loop and Math.random() to fill the array with random integer values between 0 and 100 THAT CAN BE EITHER POSITIVE OR NEGATIVE. (Hint: Subtracting numbers can produce negative numbers) b) Use a separate while loop to print each of the numbers in the array on a separate line. c) The program should then print out the number of positive integers in the array. 7

P1H P1H. Write a program that creates an array that can hold three Strings. Populate the array with name of your three favorite musical artists (Do not include Michael Jackson as one of your choices) and print the array using a for loop. Next, use Math.random() to select a random index in the array in which to place Michael Jackson, effectively overwriting one of your three favorite artists. Print the array a second time using a for loop verifying that Michael Jackson is now in the array. Use a third for loop to traverse the array and find Michael Jackson. Print a statement for each element in the array stating whether or not you found Michael. Below is sample output: 8 Use the.equals() method of the String class to check for the equality of Strings. Be sure to write your code so that if the number of elements in the array is changed, the traversal and locating code still works without having to make changes to your code. The phrase breaking your code or broken code is used when changes to your data (length of an array or contents of an array among other things) causes incorrect output by your program.

P1I P1I. Write a program to simulate a login process for a website. Allow the user to input a String for the username and an int for a password as shown below (we will address more complex passwords later.) 9 If the username AND password match the correct values, then print the following statement (or a reasonable facsimile of) using System.out.println(): You have successfully logged in If the username/password combination does not match, print the following statement (or a reasonable facsimile of) using System.out.println(): Login unsuccessful try again