CS110 Programming Language I

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

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.
Introduction to Programming
Introduction to Programming
Introduction to Programming
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Java Programming Strings Chapter 7.
1 Various Methods of Populating Arrays Randomly generated integers.
Computer Programming Lab 8.
CS1010 Programming Methodology
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
TA: Nouf Al-Harbi NoufNaief.net :::
Practice for Midterm 1. Practice problems These slides have six programming problems for in-class practice There are an additional seven programming problems.
Computer Programming Lab(4).
Computer Programming Lab(5).
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Chapter 2 Elementary Programming
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Introduction to Computers and Programming Lecture 14: User defined methods (cont) Professor: Evan Korth New York University.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Computer programming Lecture#2 أ. إلهام باسندوه 1.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
By Mr. Muhammad Pervez Akhtar
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CSC111 Quick Revision.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Elementary Programming
INC 161 , CPE 100 Computer Programming
Chapter 6 More Conditionals and Loops
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
OUTPUT STATEMENTS GC 201.
Control Statement Examples
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
An Introduction to Java – Part I, language basics
SELECTION STATEMENTS (2)
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
CS110D Programming Language I
CS 200 Primitives and Expressions
Scope of variables class scopeofvars {
More on iterations using
Computer Science Club 1st November 2019.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

CS110 Programming Language I Lab 9: Methods II Computer Science Department Spring 2014 By: TA. Nora Alaqeel

( Variables Scope ) Find Errors (If Any): class ScopeEx { public static void main(String args[]) { int num = 1; { // creates a new scope int num = 2; } } //end main } //end class By: TA. Nora Alaqeel

( Variables Scope ) Find Errors (If Any): class ScopeEx { public static void main(String args[]) { { // creates a new scope int num = 1; } int num = 2; } //end main } //end class By: TA. Nora Alaqeel

( Variables Scope ) Find Errors(If Any): class ScopeEx { public static void main(String args[]) { int n1; n1 = 10; if(n1 == 10) { // start new scope int n2 = 20; System.out.println("n1 and n2 : "+ n1 +" "+ n2); } System.out.println("n1 is " + n1); System.out.println(”n2 is " + n2); By: TA. Nora Alaqeel

( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (int firstArg, int secondArg) { ….. } Public static void myMethod (char firstArg, int secondArg) By: TA. Nora Alaqeel

( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (int firstArg, int secondArg) { ….. } Public static void myMethod (int firstArg) By: TA. Nora Alaqeel

( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (int firstArg, double secondArg) { ….. } Public static void myMethod (double firstArg, int secondArg) By: TA. Nora Alaqeel

( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (int firstArg, int secondArg) { ….. } Public static void myMethod (int first, int second) By: TA. Nora Alaqeel

( Method Overloading ) (T or F) Tell whether the following method overloading is true or false: Public static void myMethod (int firstArg, int secondArg) { ….. } Public static String myMethod (int firstArg, int secondArg) By: TA. Nora Alaqeel

Problem Description Sample Outputs: ( Returning Values + Calling ) Problem Description (Room Type) Write a program with a method named computeRoomArea which takes two float arguments entered by the user, as the length and width of a room. The method must calculate and returns the area of the room. In the main() method, you should display the result, and one of the following statements: This is a Huge Room. (If the room’s area larger than 100) This is a Tiny Room. (Otherwise – If smaller or equal to 100)   Hint: Area= length * width Sample Outputs: Please Enter length and width of the room: 12 10 The Area of the Room= 120 This is a Huge Room. By: TA. Nora Alaqeel

Code Skelton import java.util.Scanner; public class Room { public static void main(String[] args) { Scanner input = new Scanner(System.in); float length, width, area; System.out.print( "Please Enter length and width of the room: ") ; length = input.nextFloat(); width = input.nextFloat(); area = computeRoomArea(length, width); System.out.println( "The Area of the Room = " + area ); if (area > 100) System.out.println( "This is a Huge Room." ); else System.out.println( “This is a Tiny Room." ); } //end main   By: TA. Nora Alaqeel

Code Skelton Cont. public static float computeRoomArea(float l , float w) { return (l * w); } //end computeRoomArea } // end class By: TA. Nora Alaqeel

Evaluation (Print Grade) Write a program with a method named printGrade which takes one float argument entered by the user, as the user’s score. The method must returns the grade of the student. In the main() method, you should print the grade. The method header should be as the following: public static char printGrade(double score) Grade Score A Score >= 90.0 B 90.0 > Score >= 80.0 C 80.0 > Score >= 70.0 D 70.0 > Score >= 60.0 F Score < 60.0 By: TA. Nora Alaqeel

Sample Output: Please Enter Your Score: 87 Your Grade Is: B By: TA. Nora Alaqeel

Q1: Write a method named lastDigit that returns the last digit of an integer. For example, lastDigit(3572) should return 2. It should work for negative numbers as well. For example, lastDigit(-947) should return -7. Write a program to test your method.

 Q2: Write a program containing a method named countChars that receives a string read from the user, and count the number of characters in this string. Print out the result on main method.  

  Q3: Write a method named swapPairs that accepts a String as a parameter and returns that String with each pair of adjacent letters reversed. If the String has an odd number of letters, the last letter is unchanged. For example, the call swapPairs("forget") should return "ofgrte" and the call swapPairs("hello there") should return "ehll ohtree". Write a program to test your method.