CS 251 Intermediate Programming Prof. Patrick Gage Kelley University of New Mexico
Reminders • Get a CS account (at FEC 307 from George) • Labs start on Tuesday Section 1: T 12:00-12:50 — � SMLC B59 — Matthew Section 2: T 15:00-15:50 — � ESCP 110 — Jan Section 3: T 11:00-11:50 — � ESCP 110 — � Matthew • Next week live programming! • Monday, will go over updated calendar • First programming project will be out in early September August 24, 2012, 251-3
• Write a line of code that prints out: This is Answer 1. August 24, 2012, 251-3
• Write a line of code that prints out: This is Answer 1. System.out.println(“This is Answer 1.”); August 24, 2012, 251-3
• Write a method notThis that takes some text and returns that text with “not ” added to the front. If it already starts with “not” just return it. August 24, 2012, 251-3
• Write a method notThis that takes some text and returns that text with “not ” added to the front. If it already starts with “not” just return it. String notThis( String s ) { if( s.indexOf( "not " ) == 0 ) { return s; } else { return "not " + s; } } August 24, 2012, 251-3
• Write a method addUpThePositives that adds up all of the positive integers in an array that is passed into it and returns the sum. August 24, 2012, 251-3
• Write a method addUpThePositives that adds up all of the positive integers in an array that is passed into it and returns the sum. int addUpThePositives( int [] numbersToAdd ) { int sum = 0; for( int i=0; i<numbersToAdd.length; i++ ) { if( numbersToAdd[i] > 0 ) { sum += numbersToAdd[i]; } } return sum; } August 24, 2012, 251-3
• Write a method isMultipleOfTwo that takes an integer n as an argument, and that returns true if n is a multiple of two, and otherwise false. August 24, 2012, 251-3
• Write a method isMultipleOfTwo that takes an integer n as an argument, and that returns true if n is a multiple of two, and otherwise false. boolean isMultipleOfTwo( int n ) { boolean isIt = false; if( n % 2 == 0 ) isIt = true; return isIt; } August 24, 2012, 251-3
public int lotteryTicket(int a, int b, int c) { ! int worth = 0; ! if( a == b || b == c || c == a ) worth = 10; ! if( a == b && b == c ) worth = 20; ! return worth; } August 24, 2012, 251-3
• What will be the output of the following statement? for (int i=0; i<1; System.out.println(""+2+i+++i+++i)); August 24, 2012, 251-3
• What will be the output of the following statement? for (int i=0; i<1; System.out.println("" + 2 + i + ++i + ++i)); August 24, 2012, 251-3
Recommend
More recommend