Download presentation
Presentation is loading. Please wait.
Published byθάλασσα Δυοβουνιώτης Modified over 5 years ago
1
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc) Independent Practice (2.4 Assignments) Closure Activity Students will be able to: Understand what String concatenation is Understand what a primitive type is, and how it contrasts with an object of a class Understand how to return a value to the client See how today's lesson fits into the unit and the course as a whole
2
Warmup Is this a valid method? public int getNum(int x) { return 7; }
2) Declare a constant that is equal to the smallest possible int value. 3) What is the result of this code? int x = 0; for ( ; x<2; x+=1) System.out.print(x); 4) What are you most proud of in life?
3
Unit 2 Quiz: Tues 3/19
4
String concatenation is the process of joining two or more Strings together.
Using the + sign, Strings can be joined together, or chars can be added onto a String. demo: StringConcat
5
Primitive types String variables are actually objects, which are created to instantiate (or “connect to”) the String class. Most variables do not connect to any class, so these variables are not objects. These types of variables are called primitive types. There are 8 primitive types: int, double, char, boolean, short, long, float, byte.
6
The String class has methods, such as length( )
The String class has methods, such as length( ). Notice that primitive types do not have methods. For example, there is no such thing as: int x = 0; System.out.print( x.length( ) );
7
Converting an int to a String
If you want to convert an int into a String, it is slightly more complex than simple type casting. This code converts the int x into a String y: int x = 0; x = 23; String y = “ ”; y = Integer.toString(x); You might want to do this for today’s 1st assn.
8
Static Methods A static method is a method that can be called by using the name of the class itself. You do not need to create an object in order to call the method. We have been using static methods for a long time: Math.pow( ), Math.random( ), etc. This explains why you don’t have to instantiate (“connect to,” create an object of) the Math class in order to use it. Open: StaticMethodClass, StaticMethodClient
9
But what is the point of a static method? Why would you create one?
Static methods are often called utility methods: they are meant to be used without objects, because they are never going to change from one object to another. Static methods do not use instance variables. Static methods often (but not always) receive parameters, perform some action on them, and return the result.
10
Assignments P. 244 # 6 (call the files Card and CardClient)
When you create the Card class, it should have 2 methods -- one returns (doesn’t display) a card’s value (2, 3, 4, …. J, Q, K, A) and one returns (doesn’t display) a card’s suit (hearts, diamonds, spades, or clubs). Think about it – there is a way to write the Card class without using a ton of if statements (some are necessary, yes, but not a ton!) CODE EFFICIENTLY! When the instructions say “Create a program that deals 20 random cards,” you should create a client called CardClient. CardClient should call both methods, 20 times…each time you should display something like: “You have been dealt the King of Diamonds” “You have been dealt the 8 of Hearts” etc… More on next slide
11
P : (*for these assignments, only use one class, then create one client to test the methods. Call them P241Class and P241Client): # 6–9, 11, 12, 20-22
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.