CS 151 - Computer Science I
Fall-2023
Classes: MW 1:40pm - 2:55pm, Maria Sanford Hall 308
Instructor: Dr. Zdravko Markov, 30307 Maria Sanford Hall, (860)-832-2711,
http://www.cs.ccsu.edu/~markov/,
e-mail: markovz at ccsu dot edu
Office hours: MW 4:20pm - 5:30pm, TR 1:30pm-3:00pm, in person.
Book an appointment here.
Catalog description: First course in Computer Science. Introduces
the fundamental concepts of computer programming with an object-oriented
language with an emphasis on analysis and design. Topics include data types,
selection and iteration, instance variables and methods, arrays, files,
and the mechanics of running, testing and debugging.
Prerequisites: MATH 152 (may be taken concurrently)
Course Learning Outcomes:
-
Learn the basic principles of object-oriented programming and the resources
available to help them write JAVA programs (editors, JAVA libraries, on-line
help manuals, and JAVA development environments).
-
Learn the basic programming constructs, namely primitive data, variables,
assignment statement, arithmetic operators, parameter passing.
-
Learn the main control structures, namely Boolean expressions, conditional
statements, loops.
-
Understand and implement Java classes and use them in definite application
settings. Understand the ideas of abstraction and encapsulation, and incorporate
them into program design.
-
Understand and utilize text files and basic input/output operations.
-
Learn arrays of primitive data and arrays of objects.
-
Learn how to interpret non-trivial application problems (problem analysis)
and translate them into pseudo-code (program design) before implementing
them in JAVA.
Required textbook: Lewis & Loftus, Java Software Solutions:
Foundations of Program Design, 9-th Edition, Addison-Wesley, 2017.
Required software: BlueJ - an integrated Java programming environment
(http://www.bluej.org/)
Class Participation: All students are expected to attend class
sessions regularly. If you must miss a test, try to inform the instructor
of this in advance. In case of missed classes and work due to plausible
reasons (such as illness or accidents) limitted assistance will be offered.
Unexcused absences will result in the student being totally responsible
for the make-up process.
Assignments: Reading and problem assignments are listed
in the schedule of classes. Some of the problems will be worked in class.
Self-review questions, exercises and programming projects under the Assignment section
don't have to be turned in, but are recommended.
Tests: There will be 5 quizzes, two tests and a final exam.
They will include material from the textbook, the lectures, and the programming
projects.
Programming projects: There will be 6 programming projects
requiring the use of BlueJ to write and run Java programs. The projects
with their due dates are listed below in the schedule of classes (the project
descriptions are given in Blackboard). All projects must be submitted
through Blackboard.
Honesty policy: The CCSU honor code for Academic Integrity is
in effect in this class. It is expected that all students will conduct
themselves in an honest manner and NEVER claim work which is not their
own. Violating this policy will result in a substantial grade penalty,
and may lead to expulsion from the University. You may find it online at
http://web.ccsu.edu/academicintegrity/.
Please read it carefully.
Grading: The final grade will be based on projects (50%),
quizzes
(10%), tests (20%), and the final exam (20%), and will
be affected by classroom participation, conduct and attendance. All grades
will be availabe from Blackboard. The letter grades will be
calculated according to the following table:
A |
A- |
B+ |
B |
B- |
C+ |
C |
C- |
D+ |
D |
D- |
F |
95-100 |
90-94 |
87-89 |
84-86 |
80-83 |
77-79 |
74-76 |
70-73 |
67-69 |
64-66 |
60-63 |
0-59 |
Unexcused late submission policy: Projects submitted more
than two days after the due date will be graded one letter grade
down. Projects submitted more than a week late will receive
two letter grades down. No submissions will be accepted more
than two weeks after the due date.
Students with disabilities: Students who believe they need course
accommodations based on the impact of a disability, medical condition,
or emergency should contact me privately to discuss their specific needs.
I will need a copy of the accommodation letter from Student Disability
Services in order to arrange class accommodations. Contact Student Disability
Services, Willard Hall, 101-04 if you are not already registered with them.
Student Disability Services maintains the confidential documentation of
your disability and assists you in coordinating reasonable accommodations
with your faculty.
Schedule of classes, projects and tests (organized by topic, each covered in 1-3 classes)
Note: Dates for classes, tests, quizzes, and projects will be posted
at due time (see also University Calendar). Additional material may be posted. Check the schedule regularly for updates!
-
Aug 30, Sep 6: Introduction to computers and programming
-
Sep 11, 13: Java language and Object-Oriented Programming
-
Topics:
-
Writing and running Java programs
-
Comments, identifiers and reserved words
-
Program development cycle
-
Object-Oriented programming
-
Print method, character strings, escape sequences
-
Lecture slides: Chap01.pptx
-
Programs/Exercises:
-
Hello.java
-
Print "This is my first Java program" on the next line by adding another
print statement,
-
or (2) by including the message in the same print statement (escape sequence).
-
PlusOperator.java
-
Remove the parentheses enclosing the arithmetic addition and explain what
happens and why.
-
Try other parenthesizings and explain the results.
-
Print strings and arithmetic expressions with separate statements.
-
Write a program to compute the celsius equivalent of today's temperature
(use the formula C=(F-32)*5/9). Use BlueJ to create a new project and a
new class, then add the code for the main method.
-
Assignments:
-
Read Sections 1.4 - 1.6, 2.1
-
Run the programs and do the exercises described above
-
Answer Self-Review Questions: 1.21-1.26, 1.28-1.32, 2.1-2.8
-
Do Exercises: 1.2, 1.3, 1.14, 1.15, 1.17, 1.18, 1.20, 2.3-2.6
-
Do Programming Projects: 1.1, 1.2, 1.3
-
Sep 14: Take Quiz 1 (Sections 1.1-1.6, 2.1) online in Blackboard
-
Sep 18: Submit Programming Project 0
-
Sep 18, 20, 25: Data and Expressions
-
Topics:
-
Character strings
-
Primitive data types. Variables. The assignment statement and arithmetic
operators.
-
Data conversion
-
Interactive programs. The Scanner class.
-
Lecture slides: Chap02.pptx
-
Programs/Exercises:
-
Arithmetic.java
(note the difference between integer division and floating point division,
explain the modulo operation)
-
PrimitiveTypes.java
-
Explain the output
-
Print the values of the variables (combine them in the string)
-
Fix the overflow problem (two's complement representation) with long, float,
and double types and explain the difference
-
Try 2.147483647E9 instead of 2147483647 and explain what happens and why
(narrowing conversion)
-
GasMileage.java
(Listing 2.9)
-
What happens if you enter 20.5 for miles? Fix the problem in two ways.
-
What happens if enter miles and gallons on the same line?
-
Modify the program so that it reads two lines of inputs with car, miles,
and gallons (separated by blanks) and prints mpg for each car.
-
Fraction.java
(see also Rational
number,
Mixed
numbers)
-
Print the sum of the fractions as a rational number
-
Print the sum of the fractions as a mixed number number
-
Print the sum of the fractions as a floating point number
-
Explain results for a=1, b=100000001, c=1, d=100000002 (underflow)
-
Write a program, which asks the user for a change amount in cents and computes
and prints the number of quarters, dimes, nickels, and pennies to be returned.
-
Assignments:
-
Read Chapter 2
-
Run the programs and do the exercises described above
-
Answer Self-Review Questions: 2.9-2.29, 2.31-2.37
-
Do Exercises: 2.7-2.12
-
Do Programming Projects: 2.2-2.6
-
Sep 28: Take Quiz 2 (data types, assignments, data conversion, interactive programs) online in Blackboard
-
Sep 29: Submit Programming
Project 1(8 points)
-
Sep 27, Oct 2, 4: Using classes and objects
-
Topics
-
Creating objects, aliases, wrapper classes
-
The Strings class
-
The Random class, packages
-
The Math class
-
Enumerated types
-
Lecture slides: Chap03.pptx
-
Programs/Exercises:
-
StringMutation.java,
RandomNumbers.java,
Quadratic.java
-
Apply mutation4 to the original string (phrase). Explain and fix the error.
-
Write a program that reads a string and prints a random substring.
-
Write a program that generates a random quadratic equation and prints its
roots.
-
StringObjects.java
-
Write a program to read two strings and print true/false if they are equal/not
equal.
-
Extend the program with the compareTo method.
-
Translate.java
(Read a double digit number (20-99) and print it in words, e.g. 35 should
print as "thirty-five")
-
Objects.java
(make the changes and explain the results)
-
IceCream.java
(Assign chocolate flavor to cone3 and compare it with cone2 using "=="
and "equals")
-
Digits.java
(What happens with a wrong input? Extend the program to read input like
"two plus three" and print the result).
-
Assignments:
-
Read Sections 3.1 - 3.8
-
Run the programs and do the exercises described above
-
Answer Self-Review Questions: 3.1 - 3.9, 3.16 - 3.21, 3.29 - 3.31, 3.32
- 3.35
-
Do Exercises: 3.4, 3.5, 3.7 - 3.10
-
Do Programming Projects: 3.1 - 3.5
-
Oct 5: Take Quiz 3 (creating objects, aliases, wrapper classes, Strings class, Random class) online in Blackboard
-
Oct 9, 11: Decision making and Loops
-
Topics
-
Boolean expressions and logical operators
-
Comparing data and objects
-
Conditionals: the if - else and switch statements
-
Loops: the while statement
-
Lecture slides: Chap05.pptx
-
Programs/Exercises:
-
CheckInput.java
(change the intervals to open/close, use if-else to read and validate a
percentage value)
-
Write a program to read and validate a tip percentage and apply it to a
given restaurant bill.
-
Age.java
(add age intervals - kid (<=18), adult (18,65), senior (>=65), add a
birthday message)
-
See Top
25 Most Dangerous Programming Errors. Add input type and range validation
to Age.java
-
Calculator.java
(replace the if-else-if structure with a switch, note the type of the expression
in the switch statement)
-
Gcd.java
(implement the version based on subtraction)
-
Grade.java
(add input type verification)
-
Write a program to add two rational numbers (see Fraction.java) and simplify
the sum using GCD.
-
StudentGrades.java
(use a sentinel value or a question to the user for the stopping condition,
do counting and averaging in grade intervals - excellent (> 89), ok [60,89],
and failure (< 60))
-
Assignments:
-
Read Sections 5.1 - 5.4, 6.1
-
Run the programs and do the exercises described above
-
Answer Self-Review Questions: 5.1 - 5.23, 6.1 - 6.4
-
Do Exercises: 5.2 - 5.5, 5.7 - 5.11
-
Do Programming Projects: 5.1 , 5.3, 5.4
-
Oct 16: Submit Programming
Project 2 (8 points)
-
Oct 16, 18, 23: Loops continued
-
Topics
-
The do and for statements
-
Leading decision (pretest) vs. trailing decision (posttest) loops
-
Nested loops
-
Lecture slides: Chap06.pptx
-
Programs/Exercises:
-
PrintDigits.java
-
What happens with input 0? Change the while-loop to a do-loop.
-
Compute the sum of the digits of an integer.
-
Use nested loops to prove experimentally that if the sum of the digits
of a number is 9, then the number is multiple of 9
-
Loops.java
-
Move the index increment in the beginning of the while and do loops, explain
the difference
-
Print the squares from 0 to 100 with increments of 5
-
Use user specified range and increment
-
Print the multiplication table for 9 (or for a user specified number)
-
Guess.java
-
Change the range of numbers. What is the optimal strategy?
-
Include a counter to count the number of guesses
-
Change the do-loop with a while-loop
-
Series.java
-
Change the while-loop to do-loop or for-loop.
-
Use an outer loop (nested loops) for n=1,2,4,8,...,2^m to print a table
of n and sum
-
Use a conditional print (i % 100 == 0) instead of nested loops to print
the table (compare efficiency).
-
Use powers of 2 in the conditional print (Math.log(i)/Math.log(2) % 2 ==
0).
-
Find the first n for which sum >= 10.
-
Modify the series to 1/2+1/4+1/8+1/16 +... and do the convergence experiments.
See Zeno's
paradox and an interactive
demo.
-
str2int.java
(change the for-loop to while-loop, print the position of the wrong input
character)
-
Pi.java
-
Iteration.java
-
Divisors.java
-
Print the sum of the proper divisors of each number
-
For each number print if it is deficient, abundant or perfect (see these
links deficient,
abundant
or perfect).
-
Count how many numbers are deficient, abundant and perfect.
-
Primes.java
(change the for-loop to a while-loop).
-
Assignments:
-
Read Sections 6.3, 6.4
-
Run the programs and do the exercises described above
-
Answer the Self-Review Questions after Sections 6.1, 6.3, 6.4
-
Do Exercises 6.1 - 6.12
-
Do Programming Projects 6.1 - 6.3, 6.7.
-
Oct 30: Submit Programming
Project 3 (8 points)
-
Oct 25: Test 1 (Chapters 2, 3, 5, 6)
-
Oct 30, Nov 1: Writing classes
-
Topics
-
Writing user-defined classes and methods
-
Encapsulation and visibility modifiers
-
Parameters
-
Constructors, accessors, mutators
-
Instance and local data
-
Lecture slides: Chap04.pptx
-
Programs/Exercises:
-
Die.java,
RollingDice.java
(pass the initial face value as a parameter of the constructor, change
visibility modifiers to access instance variables directly, roll dice in
a loop until their face values are equal, compute probability)
-
Account.java,
Transactions.java
(write a loop to find out after how many years the balance will double,
write a method to set the interest rate)
-
Assignments:
-
Read Sections 4.1 - 4.5
-
Answer the Self-Review Questions 4.1 - 4.25
-
Do exercises 4.5 - 4.8
-
Do Programming Projects 4.1 - 4.4, 4,6, 4.9
-
Run the programs and do the exercises described above
-
Nov 6, 8: Java classes continued
-
Topics
-
Parameter passing
-
Static variables and static methods
-
Dependencies among classes, and dependencies among objects of the same
class
-
Lecture slides: Chap07.pptx
-
Programs/Exercises:
-
Rational.java,
AddRationals.java
(do the suggested exercises)
-
Series1.java
(do the suggested exercises)
-
Slogan.java,
SloganCounter.java
-
Static variables/methods: change Slogan.getCount() to obj.getCount(), remove/add
static to count and getCount()
-
Visibility modifiers: change/remove the visibility modifier of getCount())
and count,
-
Encapsulation: replace Slogan.getCount() with Slogan.count
-
Instance/local variables: add method setCount(int x), add/remove int in
int count=x
-
Assignments:
-
Read Sections 7.1 - 7.4
-
Answer the Self-Review Questions 7.6 - 7.13
-
Do exercises 7.1, 7.4, 7.11
-
Do Programming Projects 7.1, 7.2
-
Run the programs and do the exercises described above
-
Nov 9: Take Quiz 4 (Chapters 4 and 7) online in Blackboard
-
Nov 13: Submit Programming
Project 4 (8 points)
-
Nov 13: Still more Java classes
-
Topics
-
The this reference
-
Method parameters
-
Method overloading
-
Lecture slides: Chap07.pptx
-
Programs/Exercises:
-
Max.java,
Scope.java,
Num.java
(modify setValue(int value) and use the "this" reference, add static to
instance variable value)
-
Num.java,
ParameterModifier.java,
ParameterTester.java
-
try to add static to setValue() in class Num
-
add static to value in class Num and explain the output
-
add Num next; and another constructor Num(value, next) to class Num
-
create a chain (list) of numbers: Num a = new Num(0); a = new Num(1,a);
a = new Num(2,a);
-
write a loop to print all numbers along the chain
-
use a loop to create a list of numbers: Num x=null; for (int i=1;i<=10;i++)
x = new Num(i,x);
- Review of Test 2 (Chapters 4 and 7)
-
Assignments:
-
Read Sections 7.7, 7.8
-
Run the programs and do the exercises described above
-
Do exercises 7.2, 7.3, 7.5
-
Nov 15: Test 2 (Chapters 4 and 7)
-
Nov 20: Working with text files
-
Topics
-
Lecture slides: Chap05.pptx
-
Programs/Exercises:
-
Assignments:
-
Read Sections 5.5
-
Answer the Self-Review Questions 5.24, 5.25
-
Do Programming Projects 5.13 - 5.15
-
Run the programs and do the exercises described above
-
Nov 29: Submit Programming
Project 5 (8 points)
-
Nov 27, 29: Arrays
-
Topics.
- Array elements
-
Declaring and using arrays of primitive data types
-
Passing arrays as parameters
-
Lecture slides: Chap08.pptx
-
Programs/Exercises:
- Assignments:
- Read Sections 8.1, 8.2
- Answer the Self-Review Questions 8.1 - 8.13
- Do Exercises 8.1, 8.4, 8.6 - 8.10
- Do Programming Projects 8.1, 8.3, 8.5
- Run the programs and do the exercises described above
- Nov 30: Take Quiz 5 online in Blackboard for 2 points + 5 points
extra credit
-
Dec 4, 6: Arrays of objects
-
Topics
- Arrays of objects
-
Two-dimensional arrays
-
Command-line arguments
-
Lecture slides: Chap08.pptx
-
Programs/Exercises:
-
index.java
(create methods for converting a digit into word and vice versa).
-
Use command-line arguments for the user input in index.java.
-
Using two-dimensional arrays write methods for printing a matrix of integers
and for transposing a matrix.
-
Implement Students.java
using an array of objects of class Student.java
(implement a method for printing students and methods for finding/printing
students by name or grade)
-
Assignments:
-
Read Sections 8.3 - 8.4, 8.6
-
Answer the Self-Review Questions 8.14 - 8.19
-
Do Exercise 8.5
-
Do Programming Project 8.6
-
Run the programs and do the exercises described above
-
Dec 11: Submit Programming
Project 6 (10 points)
-
December 13, 1:00pm - 3:00pm: Final Exam (see Review topics and sample problems)
Programming Project 0
Log on to Blackboard
Learn to see and submit the project.
Programming Project 1
Log on to Blackboard
Learn to see and submit the project.
Programming Project 2
Log on to Blackboard
Learn to see and submit the project.
Programming Project 3
Log on to Blackboard
Learn to see and submit the project.
Programming Project 4
Log on to Blackboard
Learn to see and submit the project.
Programming Project 5
Log on to Blackboard
Learn to see and submit the project.
Programming Project 6
Log on to Blackboard
Learn to see and submit the project.
Test 1 Review Topics
The test will be based on material from Chapters 2, 3, 5, and 6 and in
particular:
-
Boolean expressions and logical operators
-
Type conversions, casting
-
Comparing data
-
If statement and conditions. Pay special attention to the syntax (the use
of ;) and to the else clause
-
Switch statement
-
While (leading decision) loops
-
Do (trailing decision) loops
-
For loops
-
Rewriting code with different loops
-
Writing programs with simple loops
Example Problems
-
Exercises: 5.2 - 5.5, 5.7 - 5.11
-
Exercises: 6.1 - 6.8
-
Print the conversion table of miles to kilometers from 0 to 100 with increments of 5. Print miles as integers and kilometers as doubles.
-
Use user specified range and increment
-
Print the multiplication table for 9 (or for a user specified number)
Test 2 Review Topics
The test will focus on classes and objects (Chapters 4 and 7), and will
also include previous material. In particular, the following topics from
Chapters 4 and 6 will be included:
-
Class structure (instance data, methods), creating and using objects.
-
Primitive data, objects, references to objects (note that Strings are objects).
-
Encapsulation and visibility modifiers.
-
Types of methods (constructors, accessors, mutators).
-
Method parameters, local variables.
-
Static methods and static variables.
-
Method signature and method overloading.
Example Problems:
-
EX 4.5 - EX 4.8 (page 185)
-
Write a class called Wallet. It has four instance variables singles,
fives, tens, and twenties (all int type). The class has one constructor
(which has a parameter corresponding to each instance variable), getter
and setter methods for each instance variable, and two service methods:
totalMoney, which returns the total dollar amount in the wallet, and getMoney,
which takes a dollar amount as a parameter and updates the number of singles,
fives, tens and twenties in the wallet. Write an application class that
contains the main method. In the main method create two Wallet objects,
and use the getter and setter method to transfer $30 from one of the wallets
to the other. Print the total amount of money in each wallet before and
after the transfer. Solution (extended): Wallet.java,
WalletRunner.java
Final Exam Review Topics
-
Variables types, arithmetic expressions, casting
-
If statements: condition, "else" and "else if" clauses.
-
Loops: for-loops, while-loops, do-loops, nested loops
-
Primitive data and objects: comparing, passing as parameters
-
Arrays: declaration, initialization, indexing, passing arrays as parameters
(note that arrays are objects), finding min/max element, arrays of objects
-
Using classes: creating objects, object reference variables
-
Creating classes: instance data and methods (visibility modifiers, encapsulation,
static/non-static), parameters, local variables, "this" reference
Example Problems: See Test 1, Test 2, Quiz 4, and Quiz 5.
Example Problem 1: You deposit $2000 in a bank that provides 1.5%
monthly interest rate. At the end of each month after applying the interest
you withdraw $150. Write a program that prints a table showing your balance
at the end of each month for 12 months. Solution: Bank.java
Example Problem 2: Write a method that takes an array of integers
as a parameter and computes and prints the sum of the squares of its elements.
Solution: IntArray.java
Example Problem 3: Write a class called Employee that contains
three instance variables representing employee s first name (String type),
last name (String type) and salary (int type), a constructor assigning
values to these variables, getter and setter methods for the salary, and a toString() method that returns the employee
information. Write another class containing the main method that declares
an array of objects of class Employee with 10 elements. Initialize each
element with an Employee object created with data entered from the keyboard
by using the Scanner class and print the array. Then find the employee with the minimal salary and increase it with 10%.
Print the employee information before and after the pay raise. Solution: Employee.java,
Company.java