Category Archives: Homework

Arrays: Magic Square

Screen Shot 2014-12-07 at 12.13.44 PM

2D Arrays

Classwork/Homework:
Magic squares. A n × n matrix that is filled with the numbers 1,2,3,…,n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
Write a program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4 × 4 array. This is only an example of a 4 x 4 square.

Screen Shot 2014-11-23 at 9.53.25 PM

You need to test two features:
1. Does each of the numbers 1, 2, …, 16 occur in the user input?
2. When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

Testing and output:
1. Display the given magic square and the value rows, columns and diagonals add up to.
2. Find one more magic square online, display it and show the value all the columns, rows and diagonals are equal to.
3. Display a square that it is not magic but it follows the rules of being filled 1 through n^2.

NOTE: print the input numbers in matrix format, rows by columns.

Strong Recommendation:
Follow the rules by tracing them on paper first. Then, find a pattern!

OOD: First Video from BlueJ

Classwork:
Chapter 1: VN 1.1 Introduction to key concepts
Take video notes.

Chapter 1: VN 1.2 Creating and using objects within BlueJ

Chapter 1: VN 1.3 methods and parameters

Assignment:

Using BlueJ’s video notes and source files on edmodo.com, create a new picture where there are more than one person in different locations, a house, a sun and grass.

Screen Shot 2015-01-13 at 10.07.43 AM house

OOD: BlueJ House

January 13th, 2015

Classwork:
Using BlueJ’s video notes and source files on edmodo.com, create a new picture where there are more than one person in different locations, a house, a sun and grass.

Screen Shot 2015-01-13 at 10.07.43 AM

If you don’t remember exactly how it was done, the video links are on yesterday’s post.

Homework:
6. Implement a class Employee. An employee has a name (a string) and a salary (a double). Write a default constructor, a constructor with two parameters (name and salary), and methods to return the name and salary. Write a small program that tests your class.

OOD Challenge Exercise

Chapter 1: VN 1.4 Solving a challenge exercise

Exercise 1:
Add a sunset to the single-sun version of Picture. That is: make the sun go
down slowly. Remember: The circle has a method slowMoveVertical
that you can use to do this.

Exercise 2: If you added your sunset to the end of the draw
method (so that the sun goes down automatically when the picture is
drawn), change this now. We now want the sunset in a separate method,
so that we can call draw and see the picture with the sun up, and then
call sunset (a separate method!) to make the sun go down.

You will find this code:
private Square wall;

private Square window;

private Triangle roof;

private Circle sun;

You need to add a line here for the second sun,

For example: private Circle sun2;

Then write the appropriate code for creating the second sun.

Exercise 3: Close the picture project, and open the lab-classes project.
This project is a simplified part of a student database designed to keep track of students in laboratory classes and to print class lists.
Create an object of class Student. You will notice that this time you
are not only prompted for a name of the instance, but also for some
other parameters. Fill them in before clicking Ok. (Remember that
parameters of type String must be written in double-quotes.)

OOD A Class Anatomy

OOD1
OOD2
OOD4
OOD5
OOD6
OOD7
OOD8
OOD9
OOD10
OOD11
OOD12

An ADT: Greeter

public class Greeter
{
   public String sayHello()
   {
      String message = "Hello, World!";
      return message;
   }
}

The tester class also called the driver:

public class GreeterTest
{
   public static void main(String[] args)
   {
      Greeter worldGreeter = new Greeter();
      System.out.println(worldGreeter.sayHello());
   }
}

An ADT: BankAccount

/**
   A bank account has a balance that can be changed by 
   deposits and withdrawals.
*/
public class BankAccount
{  
   private double balance;

   /**
      Constructs a bank account with a zero balance.
   */
   public BankAccount()
   {   
      balance = 0;
   }

   /**
      Constructs a bank account with a given balance.
      @param initialBalance the initial balance
   */
   public BankAccount(double initialBalance)
   {   
      balance = initialBalance;
   }

   /**
      Deposits money into the bank account.
      @param amount the amount to deposit
   */
   public void deposit(double amount)
   {  
      balance = balance + amount;
   }

   /**
      Withdraws money from the bank account.
      @param amount the amount to withdraw
   */
   public void withdraw(double amount)
   {   
      balance = balance - amount;
   }

   /**
      Gets the current balance of the bank account.
      @return the current balance
   */
   public double getBalance()
   {   
      return balance;
   }
}

The driver class:

/**
   A class to test the BankAccount class.
*/
public class BankAccountTester
{
   /**
      Tests the methods of the BankAccount class.
      @param args not used
   */
   public static void main(String[] args)
   {
      BankAccount harrysChecking = new BankAccount();
      harrysChecking.deposit(2000);
      harrysChecking.withdraw(500);
      System.out.println(harrysChecking.getBalance());
      System.out.println("Expected: 1500");      
   }
}

Classwork:

  1. Add a method sayGoodbye to the Greeter class.
  2. Add a method refuseHelp to the Greeter class. It should return a string such as “I am sorry, Dave. I am afraid I can’t do that.”
  3. Write a program that constructs a bank account, deposits $1000, withdraws $500, withdraws another $400, and then prints the remaining balance.
  4. Add a method
void addInterest(double rate)

to the BankAccount class that adds interest at the given rate. For example, after the statemetns

BankAccount momsSavings = new BankAccount(1000);
momsSavings.addInterest(10); // 10% interest

the balance in momsSavings is $1,100.

Assignments:

  1. Add a method sayGoodbye to the Greeter class.
  2. Add a method refuseHelp to the Greeter class. It should return a string such as “I am sorry, Dave. I am afraid I can’t do that.”
  3. Write a program that constructs a bank account, deposits $1000, withdraws $500, withdraws another $400, and then prints the remaining balance.
  4. Add a method
void addInterest(double rate)

to the BankAccount class that adds interest at the given rate. For example, after the statemetns

BankAccount momsSavings = new BankAccount(1000);
momsSavings.addInterest(10); // 10% interest

the balance in momsSavings is $1,100.

OOD: Car ADT

A class that defines an object with instance fields (attributes) and methods(behaviors) is called an ADT: Abstract Data Type

What is an abstraction?
Can you give an example of an abstraction?

“An abstraction is a general concept or idea, rather than something concrete or tangible. In computer science, abstraction has a similar definition. It is a simplified version of something technical, such as a function or an object in a program. The goal of “abstracting” data is to reduce complexity by removing unnecessary information.”
https://techterms.com/definition/abstraction

Implement a class Car_YI with the following properties.

  • A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank.
  • The efficiency is specified in the constructor, and the initial fuel level is 0.
  • Supply a method drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and methods getGas, returning the current fuel level, and addGas, to tank up.

Sample driver/test class:

Car_YI myBeemer = new Car_YI(29); // 29 miles per gallon 
myBeemer.addGas(20); // tank 20 gallons
myBeemer.drive(100); // drive 100 miles 
System.out.println(myBeemer.getGas()); // print fuel remaining


OOD – SavingsAccount

What to turn in when you submit your work in edmodo.com:
* a. Copy and paste both programs
* b. Attach both files
* c. Output should be included in your tester/driver class as a comment
* d. Test all your code: Think of data that will force execution of all the lines of code.

Classwork:
Write a class SavingsAccount_YI that is similar to the BankAccount class, except that it has an added instance variable interest. Supply a constructor that sets both the initial balance and the interest rate. Supply a method addInterest (with no explicit parameter) that adds interest to the account. Write a tester/driver program that constructs a savings account with an initial balance of $1,000 and interest rate 10%. Then apply the addInterest method five times and print the resulting balance.

bank