Chapter 2 Programming Projects - PowerPoint PPT Presentation

1 / 4
About This Presentation
Title:

Chapter 2 Programming Projects

Description:

import java.util.Scanner; class AverageOfThree ... import javax.swing.JApplet; import java.awt.*; public class ShadowName extends Japplet ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 5
Provided by: rafael9
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 Programming Projects


1
Chapter 2 Programming Projects
  • 2.1 Write an application that reads three
    integers and prints their average.
  • import java.util.Scanner
  • class AverageOfThree
  • //--------------------------------------------
    --------------------- // Reads three integers
    from the user and prints their average.
    //------------------------------------------------
    -----------------
  • public static void main (String args)
  • int num1, num2, num3
  • double average
  • Scanner sc new Scanner (System.in)
  • System.out.print ("Enter the first number
    ")
  • num1 Integer.parseInt(sc.nextLine())
  • System.out.print ("Enter the second
    number ")
  • num2 Integer.parseInt(sc.nextLine())
  • System.out.print ("Enter the third number
    ")
  • num3 Integer.parseInt(sc.nextLine())

2
Continuation of AverageOfThree.java
  • // Compute the average of num1, num2 and num3
  • average (num1num2num3) /
    3 System.out.println ("The average is "
    average)

(double)
Does this work??
NO!
Now it works!
3
Chapter 2 Programming Projects
  • 2.16 Write an applet that displays your name in
    shadow text by first drawing your name in black,
    then drawing it again slightly offset in a
    lighter color.
  • import javax.swing.JAppletimport java.awt.
  • public class ShadowName extends Japplet
  • public void paint (Graphics page)
  • setBackground (Color.yellow)
  • page.setColor (Color.gray)
    page.drawString (Ralph Tindell", 50, 50)
  • page.setColor (Color.black)
    page.drawString (Ralph Tindell", 49, 49)

4
Output of ShadowName Applet
Write a Comment
User Comments (0)
About PowerShow.com