Sunday, May 4, 2014

Assignment 10

package assignment_9_muratore;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Problems {

 // CHP 14 EX 3
 public static void division()
 {
  Scanner stdIn = new Scanner(System.in);
  int n, d, q;
  System.out.println("Enter numerator: ");
  n = stdIn.nextInt();
  System.out.println("Enter divisor: ");
  d = stdIn.nextInt();
  try {
  q = n / d;
  System.out.println(q);
  }
  catch (ArithmeticException e)
  {
   System.out.println("Error, but keep going anyway.");
  }
  stdIn.close();
 }
 
 // CHP 14 EX 4
 public static void division2()
 {
  Scanner stdIn = new Scanner(System.in);
  
  double n = 0;
  int d = 0;
  boolean OK = false;
  
  while (OK == false) {
   
   try {
    System.out.print("Enter numerator: ");
    n = stdIn.nextDouble();
    while (d == 0) {
     System.out.print("Enter divisor: ");
     d = stdIn.nextInt();
     if (d == 0) 
      System.out.println("Can not divide by 0.");
    }
    OK = true;
   }
   catch (InputMismatchException e)
   {
    System.out.println("Invalid input.. try again");
    d = 0;
    n = 0;
    OK = false;
    // required to discard data
    stdIn.next();
   }
   
  }
  

  System.out.println(n / d);
  stdIn.close();
 }
 
 // CHP 15 EX 3
 public static void testWriter()
 {
   String[] churchill = 
    {"Before Alamein we never had a victory.",
     "After Alamein we never had a defeat."};
   PrintWriter fileOut;
   try
   {
    // Append is false to start
    fileOut = new PrintWriter("elAlamein.txt");
    for (String line : churchill)
    {
     fileOut.println(line);
    }
    fileOut.close();
   }
   catch (FileNotFoundException e)
   {
    System.out.println(e.getMessage());
    
   }
 
 }
 
 // CHP 15 EX 4
 public static void appendWriter()
 {
  FileWriter fileOut;
  int year = 1942;
  try
  {
   fileOut = new FileWriter("elAlamein.txt", true);
   fileOut.write("Year " + Integer.toString(year));
   fileOut.close();
  }
  catch (IOException e)
  {
   System.out.println(e.getMessage());
  }

 }
 

 public static void main(String[] args) {
  
  System.out.println("Running Division");
  division();
  System.out.println("Running Division2");
  division2();
  System.out.println("Running testWriter");
  testWriter();
  System.out.println("Running testAppend");
  appendWriter();
  
 }

}

Tuesday, April 15, 2014

Homework 8

page 427, problem 9

1
2
3
4
5
6
7
8
static public boolean allPositive(double[] arr)
{
    for (int i = 0; i < arr.length; i++) {
       if (i < 0)
          return false;
    }
    return true;
}

page 427, problem 10

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public boolean equals(Students that)
{
    if (this.studentIds.length != that.studentIds.length)
        return false;

    for (int i = 0; i < this.students.length;i++) {
        if (this.studentsIds[i] != that.students[i])
            return false;
    }
   return true;
}

}

page 468, problem 11

i = 5, factorial = 5

This happens cause the loop never runs the factorial *=

page 468, problem 12

1
2
for (entry = ""; entry = stdIn.nextLine(); !entry.equals("q") )
  System.out.println("Enter 'q' to quit: ");

Tuesday, March 25, 2014

Java Class problems 4 and 12

Problem 4

 public void swapHardDrive(Computer otherComputer)
 {
  String temp;
  temp = this.hardDrive;
  this.hardDrive = otherComputer.hardDrive;
  otherComputer.hardDrive = temp;
 }

Problem 12

Things in bold are output

  1. Contructore without parameter is called
  2. It calls the constructor with an int parameter of 20
  3. 10
  4. 20
  5. Sets private x value to 30
  6. 30
  7. Display without an argument is called
  8. Calls display with an argument set at 50
  9. Adds 10 to it so now its 60
  10. 60
  11. Prints the old x value since non objects are passed by value.
  12. 50

Tuesday, March 4, 2014

Java Assignment 6 and 7

Problem 6

while ( (foundIndex = songs.indexOf(searchText)) != -1) {
  foundIndex += searchText.length();
  count++;
}

Problem 7

songIndex = songs.indexOf(songNum);
eolIndex = songs.indexOf("\n", songIndex);
song = songs.substring(songIndex, eolIndex);

Tuesday, February 18, 2014

IT-415 Assignment 3

Problem 3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class HelloWorld{

     public static void main(String []args){
        int count = 0;
        int sum = 0;
        int product = 1; // Needed to be 1
        do
        {
            count++;
            sum+= count;
            product *= count;
            if (count == 5) {  // Brackets were needed for if statement
                System.out.println("Sum = " + sum);
                System.out.println("Product = " + product);
            }
        } while (count < 5); // Needed semicolon
     }
}

Problem 5

line# i debug output
3 ?
4 ?
5 0
10 first
5 1
13 second
5 2
17 In default
5 3
20 i = 3