









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to various java programming concepts including using the scanner class for user input, the foreach loop construct, working with interfaces, creating classes and objects, understanding cohesion and decoupling. It includes examples of command-line arguments, handling integers, and creating applets.
Typology: Slides
1 / 15
This page cannot be seen from the preview
Don't miss anything!
1
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a double value: "); Scanner scanner = new Scanner(System.in); double d = scanner.nextDouble();
int [] a= new int[5];
foreach(int i in a){
System.out.println(“”+i);
}
2
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
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
$ javac HelloWorld.java
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