Question

signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on th
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Address.java

package testTwo;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class WrongZipCodeException extends Exception{
  
   public WrongZipCodeException(String msg) {
       super(msg);       // this will invoke constructor of Exception class
   }
}

public class Address {

   private String street, city;
   private int number, zip;
  
   public Address() {
       street = "";
       city = "";
       number = 0;
       zip = 0;
   }
  
   public Address(int number, String city, String street, int zip) {
       this.street = street;
       this.city = city;
       this.number = number;
       try{
           if(zip < 1)
               throw new WrongZipCodeException("Invalid zipcode! Zipcode has to be more than 1.");
           else
               this.zip = zip;
       }catch(WrongZipCodeException ex) {
           System.out.println(ex.getMessage());
       }
   }
   public String getStreet() {
       return street;
   }
   public void setStreet(String street) {
       this.street = street;
   }
   public String getCity() {
       return city;
   }
   public void setCity(String city) {
       this.city = city;
   }
   public int getNumber() {
       return number;
   }
   public void setNumber(int number) {
       this.number = number;
   }
   public int getZip() {
       return zip;
   }
   public void setZip(int zip) {
       this.zip = zip;
   }

   public static void main(String[] args) {
      
       int num, zip;
       String street, city;
      
       Address address1 = new Address(4032, "Debrecen", "Halasz Street", 28);
       Address address2 = new Address(3300, "Hatvan", "Alma street", 6);
       Address address3 = new Address(3700, "Kazincharcika", "Kossuth street", 40);
       Scanner in = new Scanner(System.in);       // used to read input from keyboard
      
       System.out.print("Enter house number: ");
       num = in.nextInt();
       in.nextLine();
      
       System.out.print("Enter city: ");
       city = in.nextLine();
      
       System.out.print("Enter street name: ");
       street = in.nextLine();
      
       System.out.println("Enter zip code: ");
       zip = in.nextInt();
       in.nextLine();
      
       Address address4 = new Address(num, city, street, zip);
      
       // add all address objects to a list
       List<Address> myList = new ArrayList<>();
       myList.add(address1);
       myList.add(address2);
       myList.add(address3);
       myList.add(address4);
      
       // create an object of SortedAddresses
       SortedAddress sorting = new SortedAddress();
      
       // sort by city names
       sorting.sortByCityName(myList);
       System.out.println("Sorted by city name\nZip code \t City");
       for(Address a: myList)
       {
           System.out.println(a.getZip() + "\t\t" + a.getCity());
       }
      
       // sort by zip code
       sorting.sortByZipCode(myList);
      
       // print sorted address
       System.out.println("Sorted by zip code\nZip code \t City");
       for(Address a: myList)
       {
           System.out.println(a.getZip() + "\t\t" + a.getCity());
       }
      
       in.close();
   }
}

AddressListHandler.java

package testTwo;

import java.util.List;

public interface AddressListHandler {
   public void sortByCityName(List<Address> l);
   public void sortByZipCode(List<Address> l);
}

SortedAddress.java

package testTwo;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

// this automatically sorts address based on city names
class NameSorting implements Comparator<Address>{
   @Override
   public int compare(Address a1, Address a2) {
       // here is the condition of city names sorting
       return a1.getCity().compareTo(a2.getCity());
   }
}

class ZipSorting implements Comparator<Address>{
   @Override
   public int compare(Address o1, Address o2) {
       return (o1.getZip() - o2.getZip());
   }
  
}

public class SortedAddress implements AddressListHandler{
   public void sortByCityName(List<Address> l) {
       NameSorting ns = new NameSorting();
       Collections.sort(l, ns);
   }
   public void sortByZipCode(List<Address> l) {  
       ZipSorting zs = new ZipSorting();
       Collections.sort(l, zs);
   }

}

Output:

<terminated> Address [Java Application] C:\Program Files\Java\jre\bin\javaw.exe (10-Oct-2019, 9:39:35 pm) Enter house number:

Add a comment
Know the answer?
Add Answer to:
signature 1. Create a new NetBeans Java project. The name of the project has to be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans...

    MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans IDE to create the main class called MasterMind.java MasterMind class Method main() should: Call static method System.out.println() and result in displaying “Welcome to MasterMind!” Call static method JOptionPane.showMessageDialog(arg1, arg2) and result in displaying a message dialog displaying “Let’s Play MasterMind!” Instantiate an instance of class Game() constants Create package Constants class Create class Constants Create constants by declaring them as “public static final”: public...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Java Project Create a NetBeans project for this activity. Test Stem / Question Choices 1: Create...

    Java Project Create a NetBeans project for this activity. Test Stem / Question Choices 1: Create a new class with attributes name and address. Both data type will be String. Create a constructor and assign the parameters to the attributes. Now override the toString() method. Is this possible? A: Yes. B: No. C: In some cases where the attributes are only strings. D: Yes, as long as you explicitly extends Object class. 2: Create a class with the main method...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • help please Perform the following steps: a) Using NetBeans, Create a New Java Application Project with...

    help please Perform the following steps: a) Using NetBeans, Create a New Java Application Project with Project Name BasePlusCommission Employee Test with Create Main Class check box selected. Create comments to form a descriptive header that has the course name, your name, the assignment title, and a pledge that you have neither received nor provided help to any person. Assignments submitted without this pledge will not be graded. When you have completed the steps (b) and (c) below, you will...

  • Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram)

    Create a NetBeans project called "Question 1". Then create a public class inside question1 package called Author according to the following information (Make sure to check the UML diagram) Author -name:String -email:String -gender:char +Author(name:String, email:String, gender:char) +getName():String +getEmail):String +setEmail (email:String):void +getGender():char +tostring ):String . Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f'): One constructor to initialize the name, email and gender with the given values . Getters and setters: get Name (), getEmail() and getGender (). There are no setters for name and...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • write in java and please code the four classes with the requirements instructed You will be...

    write in java and please code the four classes with the requirements instructed You will be writing a multiclass user management system using the java. Create a program that implements a minimum of four classes. The classes must include: 1. Employee Class with the attributes of Employee ID, First Name, Middle Initial, Last Name, Date of Employment. 2. Employee Type Class that has two instances of EmployeeType objects: salaried and hourly. Each object will have methods that calculates employees payrol...

  • Create a class in Java with instance variables to hold the name, street address, city, and...

    Create a class in Java with instance variables to hold the name, street address, city, and state for a user. These instance variables should be private. You should enter the name and address in the nameAddress method. You should enter the city and state in the cityState method. In the main method, print the complete address using the method printAddress. You should not use the static keyword except for the main method. Your output should be similar to the output...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT