Common Mistakes with Functions

Slides:



Advertisements
Similar presentations
Which season do you like best? 1106 Grade 8 Unit 9.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chubaka Producciones Presenta :.
2012 JANUARY Sun Mon Tue Wed Thu Fri Sat
Chapter 6 Control Structures.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
The switch statement: an N-way selection statement.
More on Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
WORD JUMBLE. Months of the year Word in jumbled form e r r f b u y a Word in jumbled form e r r f b u y a february Click for the answer Next Question.
WEATHER BY: JENNIFER FAUTH KINDERGARTEN.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
The months of the year January February. The months of the year March April.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
AP Java Java’s version of Repeat until.
July 2007 SundayMondayTuesdayWednesdayThursdayFridaySaturday
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington 1.
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Java Language Basics.
Boolean Expressions and Conditionals (If Statements)
Formatted Output (printf)
Exceptions and User Input Validation
First Programs CSE 1310 – Introduction to Computers and Programming
Dictation practice 2nd Form Ms. Micaela-Ms. Verónica.
Something about Java Introduction to Problem Solving and Programming 1.
Maximization and Minimization Problems
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
CSC 113 Tutorial QUIZ I.
McDonald’s Kalender 2009.
McDonald’s Kalender 2009.
Year 2 Autumn Term Week 12 Lesson 1
McDonald’s Kalender 2009.
2300 (11PM) September 21 Blue line is meridian..
Variables, Types, Operations on Numbers
Array Lists CSE 1310 – Introduction to Computers and Programming
Java Classes and Objects
SEASONS Khalatyan Nane Artschool The 4th grade.
McDonald’s calendar 2007.
Year 2 Autumn Term Week 12 Lesson 1
Teacher name August phone: Enter text here.
Calendar.
Take a walk down the canal and see the changes that happen as each month comes and goes. SAMPLE SLIDE Random Slides From This PowerPoint Show
First Programs CSE 1310 – Introduction to Computers and Programming
MONTHS OF THE YEAR January February April March June May July August
McDonald’s calendar 2007.
Habitat Changes and Fish Migration
Computer Science Club 1st November 2019.
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Common Mistakes with Functions Printing instead of returning. Returning the wrong value. Asking the user for input instead of using arguments. Not taking the arguments that were specified. Including extra code in your submissions ( like testing code). All these problems will be penalized severely. I have not found a better way to convince people to avoid these problems.

An Example Assignment Task File task17.java contains an incomplete program. Complete that program, by defining a season function, that satisfies the following specs: It takes one argument, called month. If month is "March", "April", or "May", the function should return "spring". If month is "June", "July", or "August", the function should return "summer". If month is "September", "October", or "November", the function should return "fall". If month is "December", "January", or "February", the function should return "winter". In all other cases, the function should return the string "unknown"

Example Main File import java.util.Scanner; public class task17 { public static void main(String[] args) Scanner in = new Scanner(System.in); while (true) System.out.printf("Enter a month, or q to quit: "); String word = in.next(); if (word.equals("q")) System.out.printf("Exiting...\n"); System.exit(0); } String s = season(word); System.out.printf("%s is in %s.\n\n", word, s); }

Example of Desired Output Enter a month, or q to quit: June June is in summer. Enter a month, or q to quit: May May is in spring. Enter a month, or q to quit: ww ww is in unknown. Enter a month, or q to quit: q Exiting...

Wrong Solution #1 What is wrong with this solution? public static String season(String month) { if (month.equals("March") || month.equals("April") || month.equals("May")) { System.out.printf("spring"); } else if (month.equals("June")||month.equals("July")||month.equals("August")) System.out.printf("summer"); else if (month.equals("September")||month.equals("October")||month.equals("November")) System.out.printf("fall"); else if (month.equals("December")||month.equals("January")||month.equals("February")) System.out.printf("winter"); else System.out.printf("unknown"); } } What is wrong with this solution?

Wrong Solution #1 What is wrong with this solution? public static String season(String month) { if (month.equals("March") || month.equals("April") || month.equals("May")) { System.out.printf("spring"); } else if (month.equals("June")||month.equals("July")||month.equals("August")) System.out.printf("summer"); else if (month.equals("September")||month.equals("October")||month.equals("November")) System.out.printf("fall"); else if (month.equals("December")||month.equals("January")||month.equals("February")) System.out.printf("winter"); else System.out.printf("unknown"); } } What is wrong with this solution? This will not even compile. It does not return anything, it should return a String.

Wrong Solution #2 What is wrong with this solution? public static String season(String month) { if (month.equals("March") || month.equals("April") || month.equals("May")) { System.out.printf("spring"); } else if (month.equals("June")||month.equals("July")||month.equals("August")) System.out.printf("summer"); else if (month.equals("September")||month.equals("October")||month.equals("November")) System.out.printf("fall"); else if (month.equals("December")||month.equals("January")||month.equals("February")) System.out.printf("winter"); else System.out.printf("unknown"); } return "";} What is wrong with this solution? It prints what it should be returning.

Output for Wrong Solution #2 Enter a month, or q to quit: June summerJune is in . Enter a month, or q to quit: May springMay is in . Enter a month, or q to quit: ww unknownww is in . Enter a month, or q to quit: q Exiting...

What Is Wrong with Solution #2 There are two ways to tell that something is wrong: 1: look at the function. It should be clear that it only returns an empty string, which violates the specs. 2: look at the output. It is clear that this output does not match the desired output.

Wrong Solution #3 What is wrong with this solution? public static void season(String month) { if (month.equals("March") || month.equals("April") || month.equals("May")) { System.out.printf("%s is in spring.\n\n", month); } else if (month.equals("June")||month.equals("July")||month.equals("August")) System.out.printf("%s is in summer.\n\n", month); else if (month.equals("September")||month.equals("October")||month.equals("November")) System.out.printf("%s is in fall.\n\n", month); else if (month.equals("December")||month.equals("January")||month.equals("February")) System.out.printf("%s is in winter.\n\n", month); else System.out.printf("unknown"); } } What is wrong with this solution?

Wrong Solution #3 What is wrong with this solution? public static void season(String month) { if (month.equals("March") || month.equals("April") || month.equals("May")) { System.out.printf("%s is in spring.\n\n", month); } else if (month.equals("June")||month.equals("July")||month.equals("August")) System.out.printf("%s is in summer.\n\n", month); else if (month.equals("September")||month.equals("October")||month.equals("November")) System.out.printf("%s is in fall.\n\n", month); else if (month.equals("December")||month.equals("January")||month.equals("February")) System.out.printf("%s is in winter.\n\n", month); else System.out.printf("unknown"); } } What is wrong with this solution? It also does not return anything. It is declared as void, so Java will not run it, unless…

Wrong Solution #3 public static void main(String[] args) { Scanner in = new Scanner(System.in); while (true) System.out.printf("Enter a month, or q to quit: "); String word = in.next(); if (word.equals("q")) System.out.printf("Exiting...\n"); System.exit(0); } season(word); Wrong Solution #3 will give the correct output, if you modify main to as shown above. MODIFYING MAIN IS NOT ALLOWED.

Wrong Solution #4 What is wrong with this solution? public static String season(String m) { Scanner in = new Scanner(System.in); System.out.printf("Enter a month, or q to quit: "); String month = in.next(); if (month.equals("March") || month.equals("April") || month.equals("May")) { return "spring"; } else if (month.equals("June")||month.equals("July")||month.equals("August")) return "summer"; else if (month.equals("September")||month.equals("October")||month.equals("November")) return "fall"; else if (month.equals("December")||month.equals("January")||month.equals("February")) return "winter"; else return "unknown"; What is wrong with this solution?

Wrong Solution #4 What is wrong with this solution? public static String season(String m) { Scanner in = new Scanner(System.in); System.out.printf("Enter a month, or q to quit: "); m = in.next(); if (month.equals("March") || month.equals("April") || month.equals("May")) { return "spring"; } else if (month.equals("June")||month.equals("July")||month.equals("August")) return "summer"; else if (month.equals("September")||month.equals("October")||month.equals("November")) return "fall"; else if (month.equals("December")||month.equals("January")||month.equals("February")) return "winter"; else return "unknown"; What is wrong with this solution? It asks the user to enter a month, instead of using the argument.

Output for Wrong Solution #4 Enter a month, or q to quit: June June is in summer. Enter a month, or q to quit: May May is in spring. Enter a month, or q to quit: ww Enter a month, or q to quit: w ww is in unknown. Enter a month, or q to quit: q Exiting...

What Is Wrong with Solution #4 Again, there are two ways to tell that something is wrong: 1: look at the function, understand that it is not using the argument. 2: look at the output, the program is asking twice for each month.

Correct Solution public static String season(String month) { if (month.equals("March") || month.equals("April") || month.equals("May")) { return "spring"; } else if (month.equals("June")||month.equals("July")||month.equals("August")) return "summer"; else if (month.equals("September")||month.equals("October")||month.equals("November")) return "fall"; else if (month.equals("December")||month.equals("January")||month.equals("February")) return "winter"; else return "unknown"; } }

Common Mistake: Variable Names public class example1 { public static int test_function(int x, int y, int z) int temporary = x*x + y*y; if (z < 0) return temporary - z*z; } else return temporary + z*z; public static void main(String[] args) int a = 3; int b = 10; int c = 8; int result = test_function(a, b, c); System.out.printf("result = %d\n", result); }} This code is correct. There is nothing wrong with it. (It doesn't matter what it does, it is a toy example).

Common Mistake: Variable Names public class example1 { public static int test_function(int x, int y, int z) int temporary = x*x + y*y; if (z < 0) return temporary - z*z; } else return temporary + z*z; public static void main(String[] args) int a = 3; int b = 10; int c = 8; int result = test_function(x, y, z); System.out.printf("result = %d\n", result); }} This code is incorrect. It will not run. Why?

Common Mistake: Variable Names public class example1 { public static int test_function(int x, int y, int z) int temporary = x*x + y*y; if (z < 0) return temporary - z*z; } else return temporary + z*z; public static void main(String[] args) int a = 3; int b = 10; int c = 8; int result = test_function(x, y, z); System.out.printf("result = %d\n", result); }} This code is incorrect. It will not run. Why? Because variables x, y, z do not exist.

Common Mistake: Variable Names public class example1 { public static int test_function(int x, int y, int z) int temporary = x*x + y*y; if (z < 0) return temporary - z*z; } else return temporary + z*z; public static void main(String[] args) int a = 3; int b = 10; int c = 8; int result = test_function(x, y, z); System.out.printf("result = %d\n", result); }} In the function call, the variable names do not have to match the names of the arguments in the function declaration.

On Testing Assignments show you examples of input and output, to help you test the code. However, testing your code is primarily YOUR responsibility. Why? whose responsibility would it be in real life? Your colleagues? Your clients? Failing on my example inputs means that the code is incorrect. Passing on my example inputs doesn't always mean that the code is correct.