CSCE 2013L: Lab 2 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CSCE 2013L: Lab 2

Description:

Lab Programs NumericTypes.java Use proper casting and order of operations to correct logic Add additional functionality: manipulate strings and calculate volume – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 16
Provided by: Ebo55
Category:
Tags: 2013l | csce | lab

less

Transcript and Presenter's Notes

Title: CSCE 2013L: Lab 2


1
CSCE 2013L Lab 2
  • Overview
  • Primitive Types in Java
  • Notes About Arithmetic Expression Types
  • Primitive Type Conversion (Casting)?
  • Java Strings
  • Input/Output (Command line and Swing)?
  • Lab Programs
  • NumericTypes.java
  • Use proper casting and order of operations to
    correct logic
  • Add additional functionality manipulate strings
    and calculate volume
  • Mileage.java
  • Write a program to calculate miles per gallon
    from scratch

2
Primitive Types in Java
  • Primitive in this case means not created from a
    class. These are the most basic types and they
    only hold a single value and have no methods.
  • Integer Types
  • byte, short, int, long (whole numbers)?
  • IEEE Floating Point Types
  • float (7 digits), double (15 digits)?
  • boolean (true or false)?
  • char (a Unicode character)?

3
Arithmetic Expressions
  • The result of a division between 2 integers is an
    integer.
  • It does not matter if you try to store it in a
    floating point variable. The fractional part will
    be missing.
  • Example
  • double result 5 / 2
  • You might expect result to contain the value 2.5,
    but it actually has 2.0
  • Integer division results in truncation

4
Arithmetic Expressions Cont.
  • In order for a division to return a floating
    point type, one of the operands must be a float.
    It does not matter which one.
  • Example
  • double result 5.0 / 2
  • double result 5 / 2.0
  • Either one of these statements gives the expected
    value of 2.5

5
Primitive Type Conversion(Casting)?
  • Java (if it is able to) allows us to convert from
    one type to the other using an operation called
    the cast.
  • The name of the type we are converting to goes in
    parentheses in front of the variable name we want
    to convert.
  • Examples
  • (double)someInteger
  • (int)someDouble
  • (int)(1.5 1.6) //my value is 3
  • (int)1.5 (int)1.6 //my value is 2

6
Java Strings
  • The String class is an example of a type Java
    provides that is not primitive.
  • We can declare a String variable with
  • String myString
  • String anotherString Another String
  • Strings have various methods that can be called
    such as length() and toUpperCase()?
  • We can call a method from an object with the .
    (dot) operator
  • int anotherLength anotherString.length()
  • The value of anotherLength is 14

7
String Operations
  • Strings can be concatenated together with the
    (plus) operator.
  • Examples
  • String first First
  • String last Last
  • String firstLast1 first last
  • String firstLast2 First Last

8
Useful String Methods
  • charAt(index)?
  • This method returns the char located at the
    position specified by int index. The numbering
    starts from 0.
  • String name Ebony
  • char initial name.charAt(0)
  • initial now holds the Unicode character for 'E'
  • toUpperCase()?
  • Returns a new String that is the uppercase
    version of the string contained in the object
  • String bigName name.toUpperCase()
  • bigName has the value EBONY

9
Output (Command Line)?
  • public class Output
  • public static void main(String args)?
  • System.out.println(Hello, world!)

10
Input (Command Line)?
  • import java.util.Scanner
  • public class Input
  • public static void main(String args)?
  • Scanner input new Scanner(System.in)
  • String name input.nextLine()

11
Output (Swing)?
  • import javax.swing.JOptionPane
  • public class OutputSwing
  • public static void main(String args)?
  • JOptionPane.showMessageDialog(null, Hello,
    world!)

12
Input (Swing)?
  • import javax.swing.JOptionPane
  • public class InputSwing
  • public static void main(String args)?
  • String name
  • JOptionPane.showInputDialog(What is your
    name?)
  • JOptionPane.showMessageDialog(null, Your
    name is name)

13
NumericTypes.java Task Summary
  • Task 1
  • Correct the errors due to incorrect order of
    operations and improper type conversions
  • Task 2
  • Use either Scanner or JOptionPane in order to get
    the user's first and last names and output their
    full name.
  • Task 3
  • Use the methods in String to get the user's
    initial and convert their name to uppercase.
  • Task 4
  • Write code to calculate the volume of a sphere

14
Mileage.java Task Summary
  • Task 5
  • Write a program from scratch following the
    guidelines in the lab manual in order to get user
    input to calculate miles per gallon.
  • Task 6
  • Following the example set by the
    NumericTypes.java, make sure your Mileage.java is
    well-commented. Be sure to provide the exact
    comments that the lab asks for.

15
Command Reference
  • How do I compile my program?
  • javac MyFile.java
  • How do I run my program?
  • java MyFile
  • How do I submit my assignment?
  • submit csce2013-005 elb02 lab2
  • if you are enrolled in the Wednesday session
  • submit csce2013-004 elb02 lab2
  • if you are enrolled in the Monday session
  • The files I require for this lab are
    NumericTypes.java, Mileage.java, and the Lab 2
    worksheet
Write a Comment
User Comments (0)
About PowerShow.com