Converting String Input to Numbers Example

/*************************************************
Name: Payroll.java
Output: Prints out total salary to the console using JOptionPane and System.out methods
Date: November 23, 2008
Author: Mr. P
*************************************************/

import javax.swing.*;
public class Payroll{ public static void main(String [] args)
{
int numOfHours;
String numOfHours_str;

double hourlyWage, totalSalary;
String hourlyWage_str;

numOfHours_str = JOptionPane.showInputDialog ("How many hours did you work this week?");
numOfHours = Integer.parseInt (numOfHours_str);

hourlyWage_str = JOptionPane.showInputDialog ("What is your hourly wage?");
hourlyWage = Double.parseDouble (hourlyWage_str);

totalSalary = numOfHours * hourlyWage;

JOptionPane.showMessageDialog (null,"Your total salary for this week is: $" +totalSalary);
System.out.println("Your total salary for this week is: $" +totalSalary);
}
}

String Literals

The String class represents text as a string of characters. Since programs usually communicate with their users through the written word, the ability to manipulate strings of text is quite important in any programming language. In some languages, strings are a primitive type, on a par with integers and characters. In Java, however, strings are objects; the data type used to represent text is the String class.
Because strings are such a fundamental data type, Java allows you to include text literally in programs by placing it between double-quote (") characters.
For example:

String name = "David";
System.out.println("Hello, " + name);

The Java Swing Classes
Java’s Swing classes create graphical objects on a computer screen. The objects can include buttons, icons, text fields, check boxes, and other good things that make windows so useful.
The name “Swing” isn’t an acronym. When the people at Sun Microsystems were first creating the code for these classes, one of the developers named it “Swing” because swing music was enjoying a nostalgic revival. And yes, in addition to String and Swing, the standard Java API has a Spring class.

Dialog Boxes

A dialog box is a small graphical window that displays a message to the user or requests input. You can quickly display dialog boxes with the JOptionPane class.

Message Dialog – A dialog box that displays a message; an OK button is also displayed.
Input Dialog – A dialog box that prompts the user for input and provides a text field where input is typed; and OK button and Cancel button are also displayed.

To display message dialogs the showMessageDialog method is used. The following statement calls that method:

JOptionPane.showMessageDialog(null, “Hello Class”);

Example:

String name;
name = JOptionPane.showInputDialog(“Enter your name”);

Converting String Input to Numbers

The JOptionPane class does not have different methods for reading values of different data types. The showInputDialog method always returns the user’s input as String, even if the user enters numeric data. For example if the user enters the number 72 into an input dialog, the showInputDialog method will return the string “72”. This can be a problem if you wish to use the user’s input in a math operation because you can not perform math on strings. In such a case, you must convert the input to a numeric value. To convert a string value to a numeric value, you use one of the methods listed below.

Examples:

1)
int number;
String str;

str = JOptionPane.showInputDialog(“Enter a number.”);
number = Integer.parseInt (str);

2)
double price;
String str;
str = JOptionPane.showInputDialog(“Enter the retail price.”);
price = Double.parseDouble (str);

_____________________________________________________

Group Presentations

Group presentations will take place on
November 26th, and 27th, 2008.
Be prepared.

_____________________________________________

Parts Of Program

/*********************************************************
Name: PartsOfProgram.java
Output: Prints out results of adding two numbers.
Demonstrates different parts of a program.
Author: Mr.P
Date: November 17th, 2008
*********************************************************/
public class PartsOfProgram //Key words (public, class)
{
public static void main (String [] args)
{
int a,b.result; //Programmer defined variables (a,b,result)
a = 10;
b = 20;
result = a + b; //Operators (+, =)

System.out.println ("The result is: " +result);
}
}

______________________________________________

Homework Assignment

Homework is due on Monday, November 17th, 2008.
1.Open an Gmail account from http://www.google.com/,
2. Using the google account's username and password sign up for blog @ http://www.blogger.com/,
3. Create your new blog for our java class,
4. Send your blog address to Mr.P,
5. Post 5 examples of each primitive data types:
int, double, char, boolean.
6. Post a message to the World embedded in a java program.

____________________________________________________

Variables in Java

In Java, you must explicitly declare all variables before using them. This rule is in contrast to some languages — most notably Basic and Visual Basic — which let you use variables that haven’t been automatically declared.
Allowing you to use variables that you haven’t explicitly declared might seem like a good idea at first glance. But it’s a common source of bugs that result from misspelled variable names. Java requires that you explicitly declare variables so that if you misspell a variable name, the compiler can detect your mistake and display a compiler error.

The basic form of a variable declaration is this:
type name;
Here are some examples:
int x;
String lastName;
double radius;

In these examples, variables named x, lastName, and radius, are declared. The x variable holds integer values, the lastName variable holds String values, and the radius variable holds double values. int variables can hold whole numbers (like 5, 1,340, and -34), double variables can hold numbers with fractional parts (like 0.5, 99.97, or 3.1415), and String variables can hold text values (like “Hello, World!” or “Jason P. Finch”).
Notice that variable declarations end with a semicolon. That’s because the variable declaration is itself a type of statement.
In short, a variable name can be any combination of letters and numerals, but must start with a letter. Most programmers prefer to start variable names with lowercase letters, and capitalize the first letter of individual words within the name. For example, firstName and salesTaxRate are typical variable names.

Initializing Variables
In Java, local variables are not given initial default values. The compiler checks to make sure that you have assigned a value before you use a local variable.

Examples of Code
/*******************************************************
Name: HelloWorld.java
Output: Prints out "Hello, world!" to the console
Date: May 3, 2006

*******************************************************/

public class HelloWorld
{
public static void main(String [] args)
{
System.out.println("Hello, World!");
}
}


/*******************************************************

Name: PartsOfProgram.java
Output: Prints out result of adding two numbers.
Demonstrates different parts of a program.
Date: May 8, 2006

*******************************************************/

public class PartsOfProgram //Key words(public, class)
{
public static void main (String [] args)
{
int a,b,result; //Programmer-defined variables(a,b,result)
a = 10;
b = 5;
result = a + b; //Operators (+,=)
System.out.println("The result is : "+result); //Punctuation (;)
}
}

__________________________________________

Primitive Data Types in Java

Integer Types

The integer types in Java are byte, short, int, and long. All integral types represent signed numbers;

Example of Primitive int
Code fragment defining, using int variables:

int secondsPerMinute = 60;
int minutesPerHour = 60;
int hoursPerDay =24;
int daysPerWeek =7;
int secondsPerWeek = secondsPerMinute *
minutesPerHour * hoursPerDay * daysPerWeek;
JOptionPane.showMessageDialog(null,
"There are " + secondsPerWeek +
" seconds per week");

int Operation Examples
int i = 5;
int j = 7;
int sumij = i + j; // 12
int diffij = i - j; // -2
int prodij = i * j; // 35
int divij = i / j; // 0
int remij = i % j; // 5
int divji = j / i; // 1
int remji = j % i; // 2

Double Types

Example of Primitive double
Code fragment:

double lbsPerKg = 2.205;
double mPerInch = 0.0254;
double height = 70.5;
double weight = 183.;
double heightMetres = height * mPerInch ;
double bmi = (weight / lbsPerKg) /
(heightMetres * heightMetres); // body mass index
System.out.println("Your body mass index is " + bmi);

double Operation Examples
double x = 5.e20; // 5.e20 = 5 * 1020
double y = 7.0e20;
double sumxy = x + y; // 1.2e21
double diffxy = x - y; // -2.0e20
double prodxy = x * y; // 3.5e41
double divxy = x / y; // .7142857142857143
double remxy = x % y; // 5.0e20
double divxy = y / x; // 1.4
double remxy = y % x; // 2.0e20

The Boolean Type
The Boolean type represents truth values. This type has only two possible values, representing the two Boolean states: on or off, yes or no, true or false. Java reserves the words true and false to represent these two Boolean values.

Example of Primitive boolean
boolean canBeCaught = true;
boolean canBeThrown = false;
What am I?

boolean goesAroundTheWorld = true;
boolean staysInACorner = true;
What am I?

The char Type
The char type represents Unicode characters.

Example of char type
char c = 'A';

________________________________________

Java

Introduction

Java is an object-oriented programming (OOP) language.
The Java programming language is the language in which Java applications, applets, servlets, and components are written. When a Java program is compiled, it is converted to byte codes that are the portable machine language of a CPU architecture known as the Java Virtual Machine (also called the Java VM or JVM). The JVM can be implemented directly in hardware, but it is usually implemented in the form of a software program that interprets and executes byte codes.

The Java Virtual Machine
It is a virtual (or "pretend") machine inside your computer. Does everything a computer is expected to such as perform calculations, manipulate memory, produce output
… but it exists only in software!

This Java Virtual Machine takes Java Byte Code instructions one by one. Translates them into machine language for the given platform. Executes the machine language and produces desired results.

The Java Virtual Machine is different for every computer that is, there is one that generates PC
instructions, one that generates Mac instructions, and so on.
It has to be a different virtual machine for each platform as every machine has a different
machine language.
You quite likely have used a Java Virtual Machine before, but did not realize it.
Most web browsers come with the Java Virtual Machine.

Processing Java - Summary
- Source code is written in Java, a high-level language
- This Java source code is run through a Java compiler
- The Java compiler doesn’t produce machine language,
but instead produces generic Java Byte Code,
-Java Byte Code is then interpreted by the Java Virtual Machine to produce desired results.
_______________________________________________________

What Is Cyberethics?

Cyberethics is the study of moral, legal, and social issues involving cybertechnology. It examines the impact that cybertechnology has for our social, legal, and moral systems. It also evaluates the social policies and laws that have been framed in response to issues generated by the development and use of cybertechnology. Hence, there is a reciprocal relationship here.

Retrieved on November 10th, 2008 from http://higheredbcs.wiley.com/legacy/college/tavani/0471249661/presentation/ch01.ppt

_______________________________________________________


Please click here to send me an e-mail.

_______________________________________________________