CompSci 4Java 1 Nov 16, 2006Prof. Susan RodgerAnnouncements• Assignment 7 out• What we will do today– Compare Alice and Java– Learn a little Java– Experiment with JavaChap. 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?Some Data Types in Java•integer – Declare and initializeint value = 0; // variable is value– Update/modifyvalue = value + 2;• Real numbersdouble number = 4.5;number = number * 2.0; // multiply by 2• Careful with operationsvalue = 6/4; // what is value?String data type in Java• String is a class• Declare String variable and initializeString phrase = “”;phrase = “CompSci 4”;• Convert String to array of charactersphrase.toCharArray()• C o m p S c i 40 1 2 3 4 5 6 7 8char type in Java• char is for one character• Note char uses single quotes, string uses double quoteschar ch = ‘a’;if (ch == ‘a’){return “found match”;}else{return “no match”;}Some String member functions• String is a class, so has member functionsphrase = “CompSci 4”;• length() - returns number of characters in Stringint size = phrase.length();• toCharArray() – converts string to array of characters and returns the array• charAt(int position) – returns the character in an array at positionchar 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.for (char ch: letters.toCharArray()){// do something here}Conditionals – Format of “if”if ( condition){ // do if condition is true}else{// do if condition is false}Relational/Logic Operators• Relational operators < > <= >= == !=•Logic Operators – && (and)– || (or)– ! (not)if ((x > 0) && (y != 3)){ // do something}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 presentProblem: 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-1What 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 PaperHow 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 AmbientEclipse (and Ambient)• See resources page on CompSci 4 web site to download Eclipse (and Ambient – built-into Eclipse)Solve this Problem• Write a method and test it on the APT– Type our solution into Eclipse– Load the file into APT and submitCreate a New Project in Eclipse• Start Eclipse• Select File -> New -> Project – Select Java Project and Next– Enter Project Name CPS4Sec1DNACreate a Class and Method• Click on project CPS4Sec1DNA– Select File -> New -> Class– Enter name DNAprofile– Select Finish– DNAprofile window appears – Cut and delete all the text from it so it is empty– Cut and paste starting code for problem– Complete the methodTesting a method using APT•Use APT to test method• All green means correct!• Class laptops – file is in C: workspaceSubmitting Java classwork for grade• In Eclipse, select “Ambient”, then “submit a project for grading”• Select “+” beside CPS 004• Then select date of classwork – classNov16• Then select today’s project to submit– CPS4SecXDNA• You will be prompted for your duke passwordSaving your work to your Duke Account• 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 saveTurn Alice code into Java Code• Select Edit Preferences• Must restart AliceClasswork today• Solve the two APTs on the CompSci 4 APT web page (create one Java project with two classes)– DNA-1 CGTA counting– DNA-2 CG counting• Submit work for grading (Ambient)– One submission for pair of partners– Include a README file in your project• Select “File” -> New -> File • enter name README.txt (both names in here)• Save files on Duke account – Ambient check in– FIRST TIME only (window -> preferences -> ambient -> checkin/checkout - setup CVS
View Full Document