1 / 21

Do Now

Do Now. Programming 1: Login to your computer. Open up Microsoft Word Think about the variables we have learned about or used. Make a list of the names Show how they are notated in Java, Write any definitions or properties of the variable that you can remember. Be prepared to share.

knoton
Download Presentation

Do Now

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Do Now Programming 1: • Login to your computer. • Open up Microsoft Word • Think about the variables we have learned about or used. • Make a list of the names • Show how they are notated in Java, • Write any definitions or properties of the variable that you can remember. • Be prepared to share. Programming 2 • Login to your computer. • Complete the Check In List and give it to Mr. N-A • Today please continue with the work that you have not marked as completed on the reflection!

  2. Variables

  3. Type of Variables TypeDescriptionKeyword character 16 bit unicode Character char booleantrue / false values boolean byte 8 bit integer numbers byte short 16 bit integer number short integer 32 bit integer number int long 64 bit integers numbers long float 32 bit floating point numbers float double 64 bit floating point numbers double

  4. Objectives • By the end of the period students will be able to: • Identify the 6 Conversions • Promotion • Conversion • Type Cast • Widening Conversion • Narrowing Conversion • Truncation • Use the following Conversions in simple student created programs.

  5. Agenda • Do Now • Powerpoint and practice • Student generated programs.

  6. Type Conversions In Expressions • Java allows you to mix different types of data together in one expression. Ex: char ch ; inti ; float f ; double outcome ; ch = ‘0’; i = 10 ; f = 10.2f ; outcome = ch * i / f ;

  7. Type Conversions In Expressions • Java allows the mixing of types within an expression because it has a strict set of conversion rules that dictate how type differences are resolved. • One portion of Java’s conversion rules is called integral promotion. • In Java whenever a char, a byte, or a short is used in an expression, it’s value is automatically elevated to int during the evaluation of that expression. • The integral promotion is only in effect during the evaluation of an expression. The variable doesn’t become physically larger. (Complier uses a temporary copy of that value)

  8. Type Conversions In Expressions • After automatic integral promotions are applied, the Java compiler converts all operands “up” to the type of the largest operand. (Type Promotion) Done on an operation-by-operation basis. • Once these conversion rules have been applied, each pair of operands will be of the same type and the result of each operation will be the same as the type of both operands. Memorize This Described by this algorithm: IF either operand is double THEN the other operand is converted to double ELSE IF either operand is a float THEN the other operand is converted to float ELSE IF either operand is long THEN the other operand is converted to long

  9. Example Make a project named Conversions Make a class named TypePromotion Type the following code: public static void main(String args[]) { inti; float f; i = 10; f = 23.25f; System.out.println (i * f); }}

  10. Type Conversions in Assignments • In assignment statements where the type of the right side differs from that of the left, the type of the right side is converted into that of the left. • When the type of the left side is larger than the type of the right side, this process causes no problems (Widening Conversions). Make a new class called Widening Conversion Then type this code: class WideningConversion { public static void main (String args [ ] ) { byte b = 127 ; inti ; i = b ; System.out.println(i) ; } }

  11. Type Conversions in Assignments • When the type of the left side is smaller than the type of the right side, compiler error results. • Ex:class BadAssignment { public static void main(String args [ ]) { byte b ; inti = 127 ; b = i ; System.out.println(b) ; } } • The compiler issues an error message about the assignment of i to b. It complains that the types are incompatible for assignment.

  12. Type Conversions in Assignments • Use a type cast to fix this problem. • Type Cast takes this general form: (type) value • Where type is the name of a valid Java data type. • Ex: The correct version of the previous program, which the output is 127. class GoodAssignment { public static void main (String args [ ]) { byte b; inti = 127; b = (byte) i ; System.out.println(b); } }

  13. Type Conversion in Assignments • Make a class called NarrowingConversion • Type the following program: Class Narrowing Conversion { public static void main (String args []) { byte b; int I = 258 b = (byte) i; System.out.println (b); } }

  14. Type Conversions in Assignments • When a float or a double is assigned to an integer type, truncation may occur. • Consider the following program, it’s output is 23 because the fractional component of f is discarded. • Make a class and type this program. class Truncation { public static void main (String args [ ] ) { float d = 23.9999 f ; inti = (int)d ; System.out.println (i) ; } }

  15. Assignment • Make a new project called Myconversions • Make 4 classes: • Promotion • Widening Conversion • Narrowing Conversion • This should have a type cast present • Truncation • Make a short program that uses a piece of data you come up with to show how to do each of the 4 Conversions. Your output should have a string that tells me what the data is and what it shows. • Use these guidelines for how much data should be used in each program. • Promotion- use 4 pieces of separate data • Widening Conversion- use 3 pieces of like data • Narrowing Conversion- use 3 pieces of like data • Use 8 pieces of like data • Example –Monthly rainfall totals

  16. Do Now Programming 1 • Take a whiteboard, a marker, and a tissue. • Login to your computer. Programming 2 • Login to your computer. • Pick up where you left off yesterday, if you are done begin arrays on the webpage. • Once Programming 1’s get started on their assignment we are going to meet.

  17. Do Now Programming 1 • Login to your computer. • Open your assignments and start working, as you finish please raise your hand for on the spot grading. Our goal is to try to have things wrapped up today if possible. Programming 2 • Login to your computer. • Pick up where you left off yesterday, if you are done begin arrays on the webpage. • Your goal is to have everything on the Webpage completed and turned in by the end of the period on Thursday, if you don’t get to arrays it is fine, it is practice getting ready to move forward on FRIDAY! If you finish arrays early try the second L.A

  18. Static Methods and Variables • There are two ways that an item can be a member of a class. • Static Member: is defined for the class itself and exist independently of any object of that class. • Instance Member: is defined for objects of the class; thus it is bound to an instance (ie object).

  19. Static Methods • Associated with a class. • To use a static method: clsName.mthName(args); • clsName is the name of the class and mthName is the name of the static method. • Ex: Math.max ( -8, -4 ); • The Math class provides several good examples of static methods.

  20. Static Variables • Also associated with a class. • Accessed through its class name as follows. clsName.varName • Here, clsName is the name of the class and varName is the name of static variable. • Ex: Math.E • The Math class provides two constants named E and PI that are static variables, which are double values.

  21. Instance Methods and Variables • Associated with and operates upon an object. • To use an instance method: objRef.mthName(args) • Here objRef is an object reference variable and mthName is the name of the method. • Ex: s.substring(0,10);

More Related