Wednesday, August 10, 2011

Assignment 10 Ch 15 number 5

5.
fileIn = new Scanner( new FileReader(stdIn.nextLine()));
while(filein.hastNext()
{
numwords++;
}

Assignment 10.Problem 4

Part A.
import java.util.Scanner;
public class JavaAp5 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner stdIn= new Scanner(System.in);
double n;
int d;
System.out.print("Enter Numerator:");
n=stdIn.nextDouble();
System.out.print("Enter divsor:");
d=stdIn.nextInt();
if (d==0)
{
System.out.print("please enter a value that is not zero");
d=stdIn.nextInt();
}
System.out.println(n/d);

Part B.
public static void main(String[] args) {
Scanner stdIn= new Scanner(System.in);
double n;
int d;
System.out.print("Enter Numerator:");
n=stdIn.nextDouble();
System.out.print("Enter divsor:");
d=stdIn.nextInt();
while(n!=0 && d!=0)
{
try
{
System.out.print("Please enter a numerator");
n=stdIn.nextDouble();
System.out.print("Please enter a denominator");
d=stdIn.nextInt();
}
catch (NumberFormatException e)
{
System.out.print("That was not a correct number");
}
}
System.out.println(n/d);
}

Friday, August 5, 2011

Chapter 13: Assignment 9

3.
Sparky= bark, bark lassie=animal

Sparky is generating a bark bark because you extended out the Animal class to Dog. and it returns a string of bark bark.

lassie is generating animal because thats what her class is calling.

8.
New Dog Lassie= animals[0]
New Cat fluffy= animals[1]

Assignment 9 Chapter 12

#8.
A. The problem will be that this method will not inherit the correct variables when another method calls it.
B. You should fix the problem by overriding that method with a .super extension.

#12.
1. Composition.
2.Composition.
3.inheritance
4. composition
5.composition.
6. inheritance
7.composition.
8.inheritance.

Chapter 11 #11-12

11. int w, x, y =z;
12. The expression Evaluates out to 'M'.

Friday, July 29, 2011

Assignment 8 #1,

Problem 6:
System.arraycopy(allSalaries,1,workerSalaries,0,7);

Problem 7:
a. It prints homer,Flanders,apu
b. change the method type to String [] and add return list;

Wednesday, July 27, 2011

P367 Number10

It is not legal to omit the pennyjar prefixes in the program.

Page 366 Problem 6.

line 21: x=3; Is incorrect because a non static variable cannot be referenced in a static method.
line 26: doIt(); Is incorrect because a non static method cannot be referenced inside of a static method.
Line 30: test.doIt(); Is incorrect because of the same reason as line 26.

Friday, July 15, 2011

Problem 7

Scanner stdIN = new Scanner(System.in);
String songs =
" 1. Bob Wow - Fresh Azimiz\n"+
"2. Weezer - Beverly Hills\n"+
"3. Dave Matthews Band - Crash Into me\n"+
"4. Sheryl Crow - Leaving Las Vegas\n";
String songNum;
int songIndex;
int eolIndex;
String song;
System.out.print("Enter song number: ");
songNum = stdIN.nextLine();
eolIndex=songNum.indexOf(songNum);
song=songNum.substring(eolIndex);
System.out.println(song);

Problem 6

public static void main(String[] args) {
// TODO code application logic here
Scanner stdIN= new Scanner(System.in);
String songs=
"1. Green Day- American Idiot\n"+
"2. Jesus Jones- Right Here\n"+
"3. Indigo Girls- Closer to Fine\n"+
"4. Peter Tosh - Equal Rights\n";
String searchText;
int foundIndex;
int count=0;
System.out.print("Enter Search text; ");
searchText=stdIN.nextLine();
if(songs.contains(searchText))
{
count++;
}
else
{
System.exit(0);
}
System.out.println("Number of ourrances of \"" +
searchText + "\" :"+count);
}
}

Tuesday, July 12, 2011

Problem # 5

for (i=2;i
{
product=num*i;
}

Problem # 3

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package p145number3;

/**
*
* @author chris.belsky
*/
public class P145number3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int count=0;
int sum=0;
int product=1;
do
{
count++;
product*=count;
sum+=count;
if(count == 5)
System.out.println("sum = " +sum);
System.out.println("Product = " + product);
}while(count<5) ;
}