Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
An object is an item created within a program from a class blueprint or description. An object is an 'instance' of a class.
private: Normally applied to class attributes not directly accessible externally.
public: Normally used for methods that allow access to attribute values.
The class implementation might look like the following:
public static void setRadius( int r){
radius = r;
}
public static void getCircumference() {
// write out the distance around its edge(formula is 2 x pi x radius)
double circumference = radius*2*3.142;
System.out.println(circumference);
}
public static void getArea( ) {
double area = 3.14*radius*radius;
System.out.println(area);
}It appears to add the integers 1–9 since i commences at 0, ends at 9 (<10) and increases in steps of one (10 values including 0). The values of i are added to sum.
The Java compiler would throw an error since sum is given no initial value and hence it is not determined.
To correct the program, add the statement int i, sum = 0; … or set sum = 0; as a separate statement.
This program seems to write out the average value of the numbers in exercise 4 by dividing the sum (45—check it!) by 10, the number of values. As before sum must be initialized at 0, but it and average are declared as integers and so the value, from integer division, is truncated and the decimal portion is lost.
Correction: declare both sum and average as float. Output would be 45.0 4.5
This program needs to include the readValue function, which we developed in the chapter, amended to return a result of type double. Here is a suitable version of the program code:
import java.io.*;
public class Currencyconversion {
// readValue function code here, amended as above
public double convert( double pounds){
double rate = 1.6;
return pounds * rate;
}
public static void main( ){
double pounds, dollars;
pounds = readValue();
dollars = convert(pounds);
System.out.println("Equivalent is $" + dollars);
}
}Use the 'x' symbol for multiply in the output—it will appear more natural than the '*' operator. Here is the fragment of code needed in the main function:
int table, product;
table = 7; // for 7 times table
for (int i = 1; i < 13; i++){
product = i * table;
System.out.println(i + "x "+ table + " = " + product);;
}Use the readValue function for integers, and replace the statement table = 7; with the following:
System.out.print("Which table ? ");
table = readValue();The complete listing is
import java.io.*;
public class CompareTwo {
// readValue utility
public static int readValue( )throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input;
int value =0;
boolean OK = false;
while (!OK){
try{
input = in.readLine();
value = Integer.parseInt(input);
OK = true;
}
catch (Exception e) {
System.out.println("Invalid - please enter integer value");
}
}
return value;
}
// the compare function
public static int compare(int one, int two){
if (one < two)
return one;
else
return two;
}
// the main function
public static void main(String [] args) throws Exception {
int first, second, smaller;
System.out.print("Enter a number: ");
first = readValue();
System.out.print("Enter a number: ");
second = readValue();
smaller = compare(first, second);
System.out.println("Smaller value is " + smaller);
}
The way to compare three (or more) values is to use a variable to hold the smaller of the first two and then compare the third with the value of the variable smallest.
The function looks like the following:
public static int compare(int one, int two, int three){
int smallest;
if (one < two)
smallest =one;
else
smallest = two;
if (three < smallest)
return three;
else
return smallest;
}The main function should read three values before passing them as
smallest = compare(first, second, third);
A longer and inefficient method would compare one with two, one with three and three with two…
This follows a similar pattern. A complete listing is given here. Note that the readValue function is not needed, since we deal with Strings:
public class CompareStrings {
public static String compare(String one, String two){
if (one.compareTo(two) <0)
return one;
else
return two;
}
public static void main(String [] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.
in));
String first, second, earlier;
System.out.print("Enter a name: ");
first = in.readLine();
System.out.print("Enter a name: ");
second = in.readLine();
earlier = compare(first, second);
System.out.println( earlier + " is alphabetically earlier");
}
}The function needs changing since no value is to be returned to main—it becomes a void function.
public class CompareStrings {
public static void compare(String one, String two){
if (one.compareTo(two) <0)
System.out.print(one +" is the alphabetically earlier");
else if (one.compare(two)>0)
System.out.print(two +" is the alphabetically earlier");
else
System.out.println("They are the same");
}Call the function from main using simply
compare(first, second);
This is a class implementation:
public class Circle {
static int radius;
public static void setRadius( int r){
radius = r;
}
public static void getCircumference() {
double circumference = radius*2*3.142;
System.out.println(circumference); }
public static void getArea( ) {
double area = 3.14*radius*radius;
System.out.println(area);
}
} // end of classA test program is as follows:
public class TestCircle {
public static void main(String[] args]{
Circle myCircle = new Circle();
myCircle.setRadius(5); // using 5.
myCircle.getArea();
myCircle.getCircumference();
}The alternative adds main as an extra function to the Circle class description itself as follows:
… Circle class as before but now add…
public static void main(String [] args){
Circle myCircle = new circle();
myCircle.setRadius(5); // using 5.
myCircle.getArea();
myCircle.getCircumference();
} // end of main
} // end of class