Warm up procedural programming exercise. Write a program that does the following
ID: 3782814 • Letter: W
Question
Warm up procedural programming exercise. Write a program that does the following: Declare a 2 dimensional ragged array of ints There is no user input. You can just copy/past these lines: int[][] table1 = {{13, 12, 53, 19}, {1, 9, 6, 25, 18, 17}, {7, 28 4}}: int[][] table 2 = {{13, 13, -85}, {11, 19}, {31, -89, 47, 26, +895}}; Write a method called count odds. The signature of the method will be like this: public static in countOdds(in[][] table) This method will return the number of elements in the array that are odd numbers. Note that the arrays might be rectangular or ragged. You might call the method from the main method like this: in odds; odds = countOdds{table}; System.out.println{"There are" + odds +" odd numbers in table 1."}; The answer should be 8. Demonstrate that it works with table2, and another table that you create. Create a class that represents a Customer. Use good OO programming technique. A Customer has a firstName, lastName, customerNumber, emailAddress. You can add some more fields if you want to, but those 4 are a minimum. override the equals method, and the toString method from the Object class. Implement the Comparable interface. In the main method, create some instances of the Customer class, and demonstrate the use of the accessors and mutator methods, as well as the compareTo method Include code and the output from running the test in your submission.Explanation / Answer
Code for 5.1
---------------------------------------------------------------------------------
public class OddCounter2D {
// here table1, table2 and table3 are declared as static becuase they are in out side of static main method.
// these can be non-static if they were declared under main method.
static int table1[][] = {
{13,12,53,19},
{1,9,6,25,18,17},
{7,28,4}
};
static int table2[][] = {
{13,13,-85},
{11,19},
{31,-89,47,26,+895}
};
static int table3[][] = {
{12,3,-8},
{10,22},
{3,-9,45,20,+5}
};
public static int countOdds(int [][]table){
int counter = 0;
for(int row=0; row<table.length; row++){
for(int col=0; col<table[row].length; col++){
if(table[row][col]%2 != 0){
counter++;
}
}
}
return counter;
}
public static void main(String[] args) {
int odds;
odds = countOdds(table1);
System.out.println("There are "+ odds +" odd numbers in table 1.");
odds = countOdds(table2);
System.out.println("There are "+ odds +" odd numbers in table 2.");
odds = countOdds(table3);
System.out.println("There are "+ odds +" odd numbers in table 3.");
}
}
Output:
There are 8 odd numbers in table 1.
There are 9 odd numbers in table 2.
There are 5 odd numbers in table 3.
---------------------------------------------------------------------------------
Code for 5.2
---------------------------------------------------------------------------------
Customer.java
public class Customer implementsparable<Customer> {
private String firstName;
private String lastName;
private int customerNumber;
private String emailAddress;
public Customer(String firstName, String lastName, int customerNumber, String emailAddress) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.customerNumber = customerNumber;
this.emailAddress = emailAddress;
}
// accessor method
public String getEmailAddress() {
return emailAddress;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getCustomerNumber() {
return customerNumber;
}
// mutator methods
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setCustomerNumber(int customerNumber) {
this.customerNumber = customerNumber;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
@Override
public String toString() {
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", customerNumber=" + customerNumber
+ ", emailAddress=" + emailAddress + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (customerNumber != other.customerNumber)
return false;
if (emailAddress == null) {
if (other.emailAddress != null)
return false;
} else if (!emailAddress.equals(other.emailAddress))
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
@Override
public intpareTo(Customer o) {
if (this.customerNumber == ((Customer) o).customerNumber)
return 0;
else if ((this.customerNumber) > ((Customer) o).customerNumber)
return 1;
else
return -1;
}
}
------------
CustomerDemo.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomerDemo {
public static void main(String[] args) {
Customer c1 = new Customer("FName1", "LName1", 20, "cone email");
Customer c2 = new Customer("FName2", "LName2", 2, "ctow email");
Customer c3 = new Customer("FName3", "LName3", 9, "cthree email");
Customer c4 = new Customer("FName4", "LName4", 3, "cfour email");
List<Customer> customerList = new ArrayList<Customer>();
customerList.add(c1);
customerList.add(c2);
customerList.add(c3);
customerList.add(c4);
System.out.println("Initial details of customers....");
for (Customer customer : customerList) {
System.out.println(customer);
}
System.out.println(); // for new line
// demo of accessor
System.out.println("getting first name of c1 " + c1.getFirstName());
System.out.println("getting customer number of c1 " + c1.getCustomerNumber());
System.out.println();
// demo of mutator
c1.setFirstName("Modified FName1");
c1.setCustomerNumber(18);
System.out.println("After calling mutator methods....");
System.out.println("getting first name of c1 " + c1.getFirstName());
System.out.println("getting customer number of c1 " + c1.getCustomerNumber());
System.out.println();
// demo ofpareTo
/*
Customer class is implementingparable interface and have methodpareTO().
Calling sort method of java.util.Collections, which internally usespareTo() method of
Customer class and sort the customers based on customerNumber.
*/
Collections.sort(customerList);
System.out.println("After callingpareTo method via Collections.sort(....)");
for (Customer customer : customerList) {
System.out.println(customer);
}
}
}
------------
Output:
Initial details of customers....
Customer [firstName=FName1, lastName=LName1, customerNumber=20, emailAddress=cone email]
Customer [firstName=FName2, lastName=LName2, customerNumber=2, emailAddress=ctow email]
Customer [firstName=FName3, lastName=LName3, customerNumber=9, emailAddress=cthree email]
Customer [firstName=FName4, lastName=LName4, customerNumber=3, emailAddress=cfour email]
getting first name of c1 FName1
getting customer number of c1 20
After calling mutator methods....
getting first name of c1 Modified FName1
getting customer number of c1 18
After callingpareTo method via Collections.sort(....)
Customer [firstName=FName2, lastName=LName2, customerNumber=2, emailAddress=ctow email]
Customer [firstName=FName4, lastName=LName4, customerNumber=3, emailAddress=cfour email]
Customer [firstName=FName3, lastName=LName3, customerNumber=9, emailAddress=cthree email]
Customer [firstName=Modified FName1, lastName=LName1, customerNumber=18, emailAddress=cone email]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.