COMP2120 Assignment 1 Solved

35.00 $

Category:

Description

5/5 - (3 votes)

 

First update the pseudocode we discussed in class (and textbook) for the account with interest rate, and then write a Java program for that to print the balance of an account after the first, second and third year, if the initial balance is $1,000 and earns 5% interest per year. Complete your code by putting a general document about your program at the top of the code as well as documents for the variables and statements inside your program.

Problem 2.

The ancient Babylonians had an algorithm for determining the square root of a number a. Start with an initial guess of a/2. Then find the average of your guess g and a/g. That’s your next guess. Repeat until two consecutive guesses are close enough, e.g. less than a threshold t. Write pseudocode for this algorithm.

Problem 3.

Write a Java program to compute and print the sum and multiplication of the first five negative integers, starting from -1, -2, …. Add document to your code.

Problem 4.

Type in the following program. Then, using the Jave API Documentation on Internet, try to complete the first two lines of the code. Then try to run the program and fix any compile-time errors you find in it. After the successful run, modify it to show a different greeting and image.

 

 

import java.net. …;              import javax. …;

import javax.swing.ImageIcon;

 

public Class Test

 

public Static main(String[] args) throws Exception {

URL imageLocation = new URL(

“http://horstmann.com/java4everyone/duke.gif”);

JOptionPane.showMessageDialog(null, “Hello”, “Title”,

JOptionPane.PLAIN_MESSAGE, new ImageIcon(imageLocation))

 

 

}

1

Problem 5.       (6 points)

Write a Java program, in which declare and initialize required variables and compute the following mathematical operations:

 

𝑔𝑡

𝑠 =              𝑠$ +       𝑣$ +        ’(                      (                                                            𝐺 = 4𝜋(       01 2.3/421

 

Problem 6.       (4 points)

What are the values of the following expressions? In each line assume that

 

double x = 2.5;       double y = -1.5;       int n = 17;       int m = 18;

 

  1. x + n * y – (x + n) * y
  2. m / n + m % n
  3. n % 2 + m % 2
  4. (m + n) / 2
  5. (m + n) / 2.0;
  6. (int) (0.5 * (m + n)
  7. 1 – (1 – (1 – n)
  8. n % 10 + (n – (n % 10))

 

  • a01-pzgqxt.zip