0% found this document useful (0 votes)
13 views6 pages

Homework 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Homework 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

The goal: A class contains multiple methods, with one method calling
another method.
Requirement: Define a class named “Calculator” that contains three
methods:
(1) The first method is named “add”, and it can calculate the sum of two
numbers.
(2) The second method is named “printSum”, it should be a non-static
method (i.e.,
instance method) with two arguments and should print the sum of two
numbers. It
needs to call the first method.
(3) The third method is the “main” function, which needs to call the second
method
and provide two actual parameters to it.

Code:

package Default;

public class Calculator {

// Adds two numbers


public int add(int a, int b) {
return a + b;
}

// Method to print the sum of two numbers


public void printSum(int a, int b) {
// Call the add method and print the sum
int sum = add(a, b);
System.out.println("The sum is: " + sum);
}

// Main method to call the printSum method


public static void main(String[] args) {
Calculator calculator = new Calculator();
// Call the printSum method with two numbers
calculator.printSum(6, 12);
}
}

2. Modify all three methods in the previous example to be static methods, and
ensure that the modified program can still run.

Code:

package Default;
class Calculator {
// Static method to calculate the sum of two numbers
public static int add(int a, int b) {
return a + b;
}

// Static method to print the sum of two numbers


public static void printSum(int a, int b) {
// Call the static add method
int sum = add(a, b);
System.out.println("The sum is: " + sum);
}

// Static main method to call printSum


public static void main(String[] args) {
// Calling printSum with two actual parameters
printSum(6,12);
}
}

3. Problem: Using Iterator to iterate over ArrayList


Requirement:
(1) Create an ArrayList<String>, add at least 5 string elements (for example:
"Apple",
"Banana", "Orange", "Grape", "Pineapple"), then use for-each loop the print
the
content of this ArrayList.
(2) Use an Iterator to iterate over the ArrayList, if an element has a length
greater
than 5, remove that element.
(3) Finally, print the modified ArrayList.
Example of the effect:
Before removal:
Apple
Banana
Orange
Grape
Pineapple
After removal:
Apple
Grape.

Code:

package Default;
import java.util.ArrayList;
import java.util.Iterator;

public class Fruits {

// Method to create
public static ArrayList<String> createFruitList() {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Grape");
fruits.add("Pineapple");
return fruits;
}

// Method to print the elements of the ArrayList


public static void printFruits(ArrayList<String> fruits) {
for (String fruit : fruits) {
System.out.println(fruit);
}
}

// Method to remove elements with length greater than 5


public static void remove(ArrayList<String> fruits) {
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
String fruit = iterator.next();
if (fruit.length() > 5) {
iterator.remove();
}
}
}

public static void main(String[] args) {


ArrayList<String> fruits = createFruitList();

// Print the list before removal


System.out.println("Before removal:");
printFruits(fruits);

// Remove fruits with length greater than 5


remove(fruits);

// Print the list after removal


System.out.println("\nAfter removal:");
printFruits(fruits);
}
}

4. Write a program that removes all numbers between 1 and 100 that are
multiples of 9 or contain the digit 9 (For example, 9 and 19 should be
removed).
Print the remaining numbers.
Hints: You can
(1) use ArrayList<Integer> to store the numbers from 1 to 100.
(2) use String.valueOf(num) to convert the int type num to a String in order
to check
if it contains the digit “9”.

Code:

package Default;

import java.util.ArrayList;

public class RemoveNumbers {

// Method to create an ArrayList of numbers from 1 to 100


public static ArrayList<Integer> createNumberList() {
ArrayList<Integer> numbers = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
numbers.add(i);
}
return numbers;
}

// Method to remove numbers that are multiples of 9 or contain the


digit 9
public static void removeUnwantedNumbers(ArrayList<Integer>
numbers) {
numbers.removeIf(num -> num % 9 == 0 ||
String.valueOf(num).contains("9"));
}

// Method to print the elements of the ArrayList


public static void printNumbers(ArrayList<Integer> numbers) {
for (int num : numbers) {
System.out.println(num);
}
}

public static void main(String[] args) {


ArrayList<Integer> numbers = createNumberList();
// Remove numbers that are multiples of 9 or contain the digit 9
removeUnwantedNumbers(numbers);

// Print the remaining numbers


printNumbers(numbers);
}
}

5. The goal: familiar with Constructor Overloading and how to use it.
Requirement: Create a Person class with two fields: name and age.
Create three different constructors:
(1) The first constructor takes no parameters, assigns the name as
"Unknown" and
the age as 0.
(2) The second constructor takes one parameter and uses it to initialize the
name.
(3) The third constructor takes two parameters and uses them to initialize
both the
name and age.
(4) Create a method named printInfo() to print the name and age of the
Person.
(5) In the main function, use three constructors to create three objects and
call
printInfo() to print their information.
(6) The program should use this keyword to assign values to the fields.

Code:

package Default;

class Person {
private String name;
private int age;

// First constructor: no parameters


public Person() {
this.name = "Unknown";
this.age = 0;
}

// Second constructor: one parameter (name)


public Person(String name) {
this.name = name;
this.age = 0;
}
// Third constructor: two parameters (name and age)
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Method to print the person's information


public void printInfo() {
System.out.println("Name: " + this.name + ", Age: " + this.age);
}

public static void main(String[] args) {


// Creating objects using different constructors
Person person1 = new Person(); // No parameter constructor
Person person2 = new Person("Dhairya"); // One parameter
constructor
Person person3 = new Person("Krisha", 18); // Two parameter
constructor

// Calling printInfo() for each object


person1.printInfo();
person2.printInfo();
person3.printInfo();
}
}

You might also like