Docsity

Java Programming: Scanner, Foreach, Interfaces, Classes, Objects, Cohesion, Decoupling, Slides of Computer Engineering and Programming

Toggle sidebar
bg1
1
Getting Input Using Scanner
1. Create a Scanner object
Scanner scanner = new Scanner(System.in);
2. Use the methods next(), nextByte(), nextShort(),
nextInt(), nextLong(), nextFloat(), nextDouble(), or
nextBoolean() to obtain to a string, byte, short, int,
long, float, double, or boolean value. For example,
System.out.print("Enter a double value: ");
Scanner scanner = new Scanner(System.in);
double d = scanner.nextDouble();
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Programming: Scanner, Foreach, Interfaces, Classes, Objects, Cohesion, Decoupling and more Slides Computer Engineering and Programming in PDF only on Docsity!

1

Getting Input Using Scanner

1. Create a Scanner object

Scanner scanner = new Scanner(System.in);

2. Use the methods next(), nextByte(), nextShort(),

nextInt(), nextLong(), nextFloat(), nextDou ble(), or

nextBoolean() to obtain to a string, byte, short, int,

long, float, d ou ble, or boolean valu e. For exam ple,

System.out.print("Enter a double value: "); Scanner scanner = new Scanner(System.in); double d = scanner.nextDouble();

Foreach Loop (new loop construct)

int [] a= new int[5];

foreach(int i in a){

System.out.println(“”+i);

}

2

Program

Development

Example 1 – Command Line Args

/*program displays command-line arguments on

*the screen.

*/

public class Program2 {

public static void main (String[] args) { System.out.println (“Hello ” + args[0]); }// end main

}// end Program2 class

Creating Applets

 Unlike Java Applications, Java Applets run in Java-enabled web browsers such as Netscape, Internet Explorer, Hot Java, etc.

 Basic steps to create an applet:

1. Create a Java source file HelloWorld.java import java.applet.Applet; import java.awt.*; public class HelloWorld extends Applet { public void paint(Graphics g) { // display message g.drawString (“ Welcome to Java ”, 25, 50); }// end paint }// end HelloWorld applet class 2. Create an HTML file to accompany your applet: hello.html A Simple Program Here is the output of my program:

Docsity.com

Creating Applets: (Contd.)

  1. Compile the source file

$ javac HelloWorld.java

  1. Run the program $ appletviewer hello.html

Choosing Classes

 A class represents a single concept

  • Concepts from mathematics: Point Rectangle Ellipse
  • Concepts from real life BankAccount Purse
  • Actors StringTokenizer Random (better called RandomNumberGenerator)
  • Utility classes--no objects, only static methods Math

Cohesion

 Cohesive = public interface closely related to the single concept that the class represents

 This class lacks cohesion: public class Purse { public Purse(){...} public void addNickels(int count){...} public void addDimes(int count){...} public void addQuarters(int count){...} public double getTotal(){...}

public static final double NICKEL_VALUE =0.05; public static final double DIME_VALUE =0.1; public static final double QUARTER_VALUE =0.25; ... }

Decoupling

 A class depends on another if it calls

one of its methods

 Purse depends on Coin because it

calls getValue on coins

 Coin does not depend on Purse

 High Coupling = many class

dependencies

 Minimize coupling to minimize the

impact of interface changes

Dependency Relationship between

Purse and Coin Classes