CHAPTER 4

Site hosted by Angelfire.com: Build your free website today!

Built-in classes(Standard Classes)


Math Class
- Math.abs(int x) //absolute value
- Math.abs(double x)
overloading - two methods wotj tje same name and different parameters. Parameter type determine which method will be called.
- Math.pow(double base, double exponent)
- Math.round(double x) //rounds the number to an integer
- Math.max(int a, int b) //returns the highest number
- Math.min(int a, int b) //returns the lowest number
- Math.PI
//returns the constant pi

Random Class (not built in)


import java.util.Random;
Random generator=new Random(); - nextInt(int n) //random integer from 0 to n-1
- nextDouble() //random decimal from 0 to 1.0


String methods


indexOf(aString, int begin)
indexOf(aString)
indexOf(aCharacter, int begin)
indexOf(aCharacter)
- returns an integer position(-1 if not found)
- begin is an integer position
length()
toUpperCase()
toLowerCase()
equals(String other)
- boolean
- case sensitive
Ex.
String word1, word2;
word1=reader.readLine("Enter a word ");
word2=reader.readLine("Enter another word ");
boolean isSame;
isSame=word1.equals(word2);
equalsIgnoreCase
- not case sensitive
trim()
- cuts off leading and trailing spaces
charAt()
- returns a character at a given position
substring(start, end)
- returns a section of the calling string from start position up to but not including the ending position
compareTo(String other)
- case sensitive
- returns(-) if calling string comes before parameter
- returns(+) if calling string comes after parameter
- returns (0) if they are equal

The Conditional Statement


 
	if (condition)
		statement;
	
	if (condition)
	{
		statement1;
		statement2;
	}

Conditional Operators
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
- == is equal to
- != not equal

Else If


	if (condition)
		statement;
	else if (condition2)
		statement;
	else			//**else is required**
		statement;

Comparing String objects


Can only compare methods that return a primitive value type
- .equals
- .charAt

Logic Operators


&& and
|| or
! not


Sample Programs:



/***********************************************
chapter 4 program #4
per 3
***********************************************/
import TerminalIO.KeyboardReader;

public class CH4PROG4
{
	public static void main(String [] args)
	{
		KeyboardReader reader=new KeyboardReader();
		String before, after;
		int locationOfHate;
		
		System.out.println("Enter a line of text: ");
		before=reader.readLine();
		locationOfHate=before.indexOf("hate");
		after=before.substring(0,locationOfHate);
		after+="love";
		after+=before.substring(locationOfHate+4,before.length());
		System.out.println(after);
	}
}

/**********	OUTPUT ************

Enter a line of text:
I hate this computer
I love this computer
Press any key to continue...

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



/***********************************************
chapter 4 program #5
per 3
***********************************************/

import TerminalIO.KeyboardReader;

public class CH4PROG5
{
	public static void main(String [] args)
	{
		KeyboardReader reader=new KeyboardReader();
		String before, after;
		int lengthWord;
		String replacement;
		
		System.out.println("Enter a line of text (no puncuation please): ");
		before=reader.readLine();
		lengthWord=before.indexOf(' ', 0);
		after=before.substring(lengthWord+1, before.length());	//puts takes ladder half of entry
		after+=" " + before.substring(0,lengthWord);			//puts first word last
		System.out.println(after);
	}
}

/*	OUTPUT	**************************************
Enter a line of text (no puncuation please):
my name is Mike
name is Mike my
Press any key to continue...
*************************************************/


/**********************
ch 4 Random Number
per 3
***********************/

import TerminalIO.KeyboardReader;
import java.util.Random;

public class RandomNum
{
	public static void main(String [] args)
	{
		KeyboardReader reader=new KeyboardReader();
		Random generator=new Random();
		int randomNum=generator.nextInt(66)+2, sqrNumToDisplay;
		double num1, num1Rounded, num1Abs, numToDisplay, numToPow;
		
		System.out.println("The random number is: "+randomNum);
		
		num1=reader.readDouble("Enter any number: ");
		System.out.println("The number is: "+num1);
		
		num1Rounded=Math.round(num1);
		System.out.println("The number rounded is: "+num1Rounded);

		num1Abs=Math.abs(num1Rounded);
		System.out.println("The number in absolute value is: "+num1Abs);

		numToDisplay=Math.min(randomNum, num1Abs);
		System.out.println("Betweent the number above, and a random number, this is the lowest: "+numToDisplay);

		sqrNumToDisplay=(int)Math.round(Math.sqrt(numToDisplay));
		System.out.println("The square root of the above is: "+sqrNumToDisplay);
		
		numToPow=Math.pow(num1Abs, (double)sqrNumToDisplay);
		System.out.println("The absolute value of the rounded off number you entered raised to the above is: "+num1Abs+"^"+sqrNumToDisplay+"="+numToPow);
	}
}

/*	OUTPUT	*********************************************************************
The random number is: 64
Enter any number: -3.64
The number is: -3.64
The number rounded is: -4.0
The number in absolute value is: 4.0
Betweent the number above, and a random number, this is the lowest: 4.0
The square root of the above is: 2
The absolute value of the rounded off number you entered raised to the above is:
 4.0^2=16.0
Press any key to continue...
*********************************************************************************/


/*******************************
Extra Credit 10-4-02
per 3
*******************************/

import TurtleGraphics.StandardPen;

public class ExtraCredit1
{
	public static void main(String [] args)
	{
		StandardPen pen=new StandardPen();
		
		pen.down();
		pen.turn(-90);
		pen.move(20*2);
		
		pen.turn(135);
		pen.move(28*2);
		
		pen.turn(-90);
		pen.move(14*2);
		
		pen.turn(-90);
		pen.move(14*2);
		
		pen.turn(-90);
		pen.move(28*2);
		
		pen.turn(-135);
		pen.move(20*2);
		
		pen.turn(-90);
		pen.move(20*2);
		
		pen.turn(-90);
		pen.move(20*2);
	}
}


/**************************
project 4.4
***************************/

import TerminalIO.KeyboardReader;

public class Project44
{
	public static void main(String [] args)
	{
		KeyboardReader reader=new KeyboardReader();
		int numtimes=4;
		double piAprox=0;
		
		numtimes=reader.readInt("How many times to you want to run the iteration? ");
		
		for (int x=0, y=1;x<=numtimes-1;x++, y+=2)
			piAprox+=1/(Math.pow(-1,x)*y);
		piAprox*=4;
		
		System.out.println("The approximation for pi is "+piAprox);
	}
}

/******OUTPUT*****************
How many times to you want to run the iteration? 20
The approximation for pi is 3.09162380666784
Press any key to continue...

How many times to you want to run the iteration? 1000
The approximation for pi is 3.140592653839794
Press any key to continue...
*****************************/


/*************************
Kevin M
per 3
Project 4.5
*************************/

import TerminalIO.KeyboardReader;

public class Project45
{
	public static void main(String [] args)
	{
		KeyboardReader reader=new KeyboardReader();
		
		int totalPop, numOrgStart=reader.readInt("Enter the number of organisms at the start: ");
		double rateGrowth=reader.readDouble("Enter the rate of growth: ");
		double numHoursForRate=reader.readDouble("Enter the number of hours to acheive this rate: ");
		double numHoursGrowing=reader.readDouble("Enter the number of hours it is growing: ");
		
		
		totalPop=(int)(numOrgStart*rateGrowth* numHoursGrowing/numHoursForRate);
		System.out.println("The total population after "+numHoursGrowing+" hour(s) will be "+totalPop);
	}
}
/*****OUTPUT*****************************************
Enter the number of organisms at the start: 500
Enter the rate of growth: 2
Enter the number of hours to acheive this rate: 6
Enter the number of hours it is growing: 12
The total population after 12.0 hour(s) will be 2000
Press any key to continue...

Enter the number of organisms at the start: 100
Enter the rate of growth: 6
Enter the number of hours to acheive this rate: .5
Enter the number of hours it is growing: 24
The total population after 24.0 hour(s) will be 28800
Press any key to continue...
*****************************************************/


Home