CompSci 4 Java 1 Apr 2, 2009 Prof. Susan Rodger. Announcements Assignment 7 questions? –Beware having two events that kick in at the same time! –Beware.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CompSci 101 Introduction to Computer Science February 3, 2015 Prof. Rodger Lecture given by Elizabeth Dowd.
Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Alice Variables Pepper. Set to Java look Edit / preferences restart.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Javascript and Basic Programming Concepts. What is a Program? A program is a specific set of instructions written in a computer language to perform a.
CIS Computer Programming Logic
CompSci 100e Program Design and Analysis II January 18, 2011 Prof. Rodger CompSci 100e, Spring20111.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
CompSci 101 Introduction to Computer Science September 23, 2014 Prof. Rodger.
CompSci 101 Introduction to Computer Science Sept. 9, 2014 Prof. Rodger President Brodhead speech graduation 2010 CompSci 101 Fall
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
Applications Development
Objective: Students will be able to: Declare and use variables Input integers.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
CompSci 6 Introduction to Computer Science September 13, 2011 Prof. Rodger.
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
CompSci 4 Starting Alice Jan 15, 2009 Prof. Susan Rodger Alice is named in honor of Lewis Carroll’s Alice in Wonderland.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
CompSci 101 Introduction to Computer Science January 28, 2016 Prof. Rodger compsci101 spring161.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CompSci 101 Introduction to Computer Science January 26, 2016 Prof. Rodger compsci 101, spring
CompSci 101 Introduction to Computer Science November 11, 2014 Prof. Rodger CompSci 101 Fall Review for exam.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CompSci 6 Introduction to Computer Science September 27, 2011 Prof. Rodger CompSci 6 Fall
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
CompSci 101 Introduction to Computer Science February 5, 2015 Prof. Rodger Lecture given by Elizabeth Dowd compsci101 spring151.
CompSci 101 Introduction to Computer Science Sept 13, 2016 Prof. Rodger compsci101 fall161.
Repetition Statements
7 - Programming 7J, K, L, M, N, O – Handling Data.
By Melissa Dalis Professor Susan Rodger Duke University June 2011
Week 2 - Wednesday CS 121.
CprE 185: Intro to Problem Solving (using C)
CompSci 101 Introduction to Computer Science
Eclipse Navigation & Usage.
CSIS 1117A Computer Programming (C++)
CompSci 101 Introduction to Computer Science
CS 115 Lecture 8 Structured Programming; for loops
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
Programming Language Concepts (CIS 635)
CompSci 101 Introduction to Computer Science
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CompSci 101 Introduction to Computer Science
CompSci 101 Introduction to Computer Science
An Introduction to Java – Part I, language basics
CompSci 101 Introduction to Computer Science
Alice Variables Pepper.
CS 200 Loops Jim Williams, PhD.
Fundamentals of Functional Programming
Introduction to Computer Science
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
CSS161: Fundamentals of Computing
COMPUTING.
Presentation transcript:

CompSci 4 Java 1 Apr 2, 2009 Prof. Susan Rodger

Announcements Assignment 7 questions? –Beware having two events that kick in at the same time! –Beware of infinite loops! What we will do today –Compare Alice and Java –Learn a little Java –Experiment with Java

Chap. 11 – What’s Next? Java Java – object-oriented programming language –Classes, objects, inheritance –Control structures (if, while) –Functions, methods –Data types (integers, doubles, strings, arrays, lists) Sound familiar?

Turn Alice code into Java Code Select Edit Preferences Must restart Alice

Some Data Types in Java integer –Declare and initialize int value = 0; // variable is value –Update/modify value = value + 2; Real numbers double number = 4.5; number = number * 2.0; // multiply by 2 Careful with integer operations value = 6/4; // what is value?

String data type in Java String is a class Declare String variable and initialize String phrase = “” ; phrase = “ CompSci 4 ” ; Convert String to array of characters phrase.toCharArray() C o m p S c i

char type in Java char is for one character Note char uses single quotes, string uses double quotes char ch = ‘ a ’ ; if (ch == ‘ a ’ ) { return “ found match ” ; } else { return “ no match ” ; }

Some String member functions String is a class, so has member functions String phrase = “ CompSci 4 ” ; length() - returns number of characters in String int size = phrase.length(); toCharArray() – converts string to array of characters and returns the array charAt(int position) – returns the character in an array at position char ch = phrase.charAt(2);

Looping over a String Collections loop – converts the String letters to a character array and iterates over the array with ch being one character from the array each time. Like Alice, getting one item-from-list at a time for (char ch: letters.toCharArray()) { // do something here with ch } Must have Java 1.5 for collections loop! type item_from_list Name of string Convert to array

Example – what does this do? int sum = 0; String phrase = “3 weeks left”; for (char ch: phrase.toCharArray()) { sum = sum + 1; }

Looping over a String – Java 1.4 or less Can’t use Collections loop Use for loop instead – like complicated loop in Alice Like Alice, getting one item-from-list at a time Assume string variable is called words for (int item=0; item< words.length(); item = item+1) { // do something here with words.charAt(item) // that is one character from words at a time }

Conditionals – Format of “if” if ( condition) { // do if condition is true } else // can leave off if no else part { // do if condition is false } Must have ( )’s around condition! Can leave “else” part off

Relational/Logic Operators Relational operators = == != Logic Operators –&& (and) –|| (or) –! (not) if ((x > 0) && (y != 3)) { // do something }

Example – what does this do? String letters = “CompSci 4 rocks”; int sum = 0; for (char ch: letters.toCharArray()) { if (ch == ‘S’ || ch == ‘s’) { sum = sum + 1; }

Problem 1 to Solve in Java Bioinformatics –Area of computer science –Application of computational techniques to the management and analysis of biological information Problem: Given a strand of DNA, determine the number of cytosine nucleotides present

Problem: Rewritten for CompSci DNA is a string – array of characters –Only has letters c, t, a and g Problem restated: how many c’s in a string? Example: “catacgtatagtc” –Answer: 3 c’s Write a method to return this number –See sheet for problem DNA-1

What does code mean? Name of class Name of method in class Return value (int is integer or number) One parameter (type and name)

Solve Problem on Paper

How We Will Solve Problems in Java Write methods and test with testing interface:APT –Not a whole Java program, just a small part Write a complete Java program –Not yet Use a programming environment Eclipse to make it easier Use submission tool Ambient See CompSci 4 resources page to install!

Solve this Problem Write a method and test it on the APT –Type our solution into Eclipse –Load the file into APT (web page) and test/run

Create a New Project in Eclipse Start Eclipse Select File -> New -> Project –Select Java Project and Next –Enter Project Name CPS4Sec1DNA –Think of project as an Alice world with lots of classes

Create a Class and Method Click on project CPS4Sec1DNA –Select File -> New -> Class –Enter name DNAprofile –Select Finish –DNAprofile window appears –Cut and paste the method “count” from the web page to the class –Complete the method Put all classes you create today in the same project!

Testing a method using APT Use APT to test method Select problem, load file, test/run. Class laptops – file is in C: workspace

Want Green, not red! Execution of the apt

Debugging your program Scroll down to see more detail Shows expected value, calculated value, and input value

Saving your work to your Duke Account – if on class laptop Check in your project by selecting “Ambient”, “Check in project” First time only (Window -> preferences -> ambient -> checkin/checkout -> setup CVS) Enter your Duke account password If partner wants to save after one has saved, must click on project, select “Team”, then “disconnect”, then partner can try to save

Classwork today Solve the three APTs on the CompSci 4 APT web page (create one Java project with three classes) –DNA-1 CGTA counting –DNAcgdiff –DNA-2 CG counting Get work checked off – show runs and code If on class laptop, save files on Duke account –Ambient check in –FIRST TIME only (window -> preferences -> ambient - > checkin/checkout - setup CVS repository