Question

Write a complete Java application according to the following specifications: The application is built with Ant...

 
 Write a complete Java application according to the following specifications:

    The application is built with Ant and uses gson-2.8.5.jar (or later), which must be included properly in the NetBeans-exported zip file you submit to Canvas
    The application has at least the following four source-code files: StudentInfoGsonApp.java, Student.java, StudentList.java, PhoneNumber.java
    The main method in StudentInfoGsonApp
        creates the studentInfo String array as follows:

                  String[] studentInfo = {"John, Doe, 3.1, 866-555-1212, Rust;Julia",
                    "Jane, Deere, 3.25, 898-555-1212, swimming;sleeping;dreaming;kayaking",
                    "Sam, Spade, 2.9, 888-555-1212, coffee-drinking;Java;Python"};
                  

        creates a new StudentList Object and populates that StudentList Object with three Student Objects that take their values from studentInfo array above, according to these specifications:
            The phone number in each Student Object is an Object of type PhoneNumber, and that Object has three integer fields (for area code, prefix, and line number) into which the each phone number gets parsed.
            The skills variable in each Student Object is an array of Strings containing that Student's skills, which are listed as the semicolon-separated items at the end of each student in the above studentInfo array. 
        creates a new Gson Object (ready for pretty printing) and uses a method of that Object to convert your StudentList Object to a JSON String.
        converts that JSON String back to a *new and different* Java Object of type StudentList
        prints the output below by:
            first pretty-printing the JSON String you created above, and
            then iterating through the *new and different* Java StudentList Object you created to print its values exactly as shown below. Syntax counts here. 

        {
          "students": [
            {
              "firstName": "John",
              "lastName": "Doe",
              "gpa": 3.1,
              "phoneNumber": {
                "areaCode": 866,
                "prefix": 555,
                "lineNum": 1212
              },
              "skills": [
                "Rust",
                "Julia"
              ]
            },
            {
              "firstName": "Jane",
              "lastName": "Deere",
              "gpa": 3.25,
              "phoneNumber": {
                "areaCode": 898,
                "prefix": 555,
                "lineNum": 1212
              },
              "skills": [
                "swimming",
                "sleeping",
                "dreaming",
                "kayaking"
              ]
            },
            {
              "firstName": "Sam",
              "lastName": "Spade",
              "gpa": 2.9,
              "phoneNumber": {
                "areaCode": 888,
                "prefix": 555,
                "lineNum": 1212
              },
              "skills": [
                "coffee-drinking",
                "Java",
                "Python"
              ]
            }
          ]
        }

        [firstName: John, lastName: Doe, gpa: 3.1, phoneNumber=866-555-1212, skills: [Rust,Julia]]
        [firstName: Jane, lastName: Deere, gpa: 3.25, phoneNumber=898-555-1212, skills: [swimming,sleeping,dreaming,kayaking]]
        [firstName: Sam, lastName: Spade, gpa: 2.9, phoneNumber=888-555-1212, skills: [coffee-drinking,Java,Python]]
                  

Homework Answers

Answer #1

-------------------------------------------Student.java--------------------------------


public class Student{
        private String firstName;
        private String lastName;
        private float gpa;
        private PhoneNumber phoneNumber;
        private String skills[];
        // constructor
        public Student(String f,String l,float g,PhoneNumber p,String s[]) {
                firstName=f;
                lastName=l;
                gpa=g;
                phoneNumber=p;
                skills=s;
        }
        @Override
        public String toString() {
                String str="[firstName: "+firstName+", lastName: "+lastName+", gpa: "+gpa+", phoneNumber="+phoneNumber+", skills: [";
                for(String s:skills) {
                        str+=s+",";
                }
                str=str.substring(0,str.length()-1);
                str+="]]";
                return str;
        }
}

----------------------------------------PhoneNumber.java---------------------------------------

public class PhoneNumber{
        private int areaCode;
        private int prefix;
        private int lineNum;
    // constructor
        public PhoneNumber(int a,int p,int l) {
                areaCode=a;
                prefix=p;
                lineNum=l;
        }
        @Override
        public String toString() {
                return areaCode+"-"+prefix+"-"+lineNum;
        }
}

-----------------------------------------------------StudentList.java---------------------------

class StudentList{
        private ArrayList<Student> students;
    // constructor
        public StudentList() {
                students=new ArrayList<>();
        }
        public StudentList(StudentList l) {
                students=new ArrayList<>();
                for(Student s:l.students) {
                        students.add(s);
                }
        }
    // function to add Student to students list
        public void add(Student s) {
                students.add(s);
        }
    // function to display Student objects of students list
        public void display() {
                for(Student st:students) {
                        System.out.println(st);
                }
        }
}

----------------------------------------StudentInfoGsonApp.java--------------------------------

import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder; 

public class StudentInfoGsonApp {
        public static void main(String args[]) {
                String[] studentInfo = {"John, Doe, 3.1, 866-555-1212, Rust;Julia",
                "Jane, Deere, 3.25, 898-555-1212, swimming;sleeping;dreaming;kayaking",
                "Sam, Spade, 2.9, 888-555-1212, coffee-drinking;Java;Python"};
                
                Gson g=new GsonBuilder().setPrettyPrinting().create();// gson object for pretty printing
                StudentList dd=new StudentList();
                for(int i=0;i<studentInfo.length;i++) {// iterate studentInfo array to create Student objects
                        String m[]=studentInfo[i].split(", ");
                        
                        String mm[]=m[3].split("-");
                        PhoneNumber p=new PhoneNumber(Integer.parseInt(mm[0]),Integer.parseInt(mm[1]),Integer.parseInt(mm[2]));
                        
                        String s[]=m[4].split(";");
                        
                        Student ob=new Student(m[0],m[1],Float.parseFloat(m[2]),p,s);
                        dd.add(ob);
                }
                String json=g.toJson(dd);// java object to json string
                System.out.println(json);
                StudentList l=g.fromJson(json,StudentList.class);// json string to java object
                StudentList newl=new StudentList(l);
                newl.display();
                
        }
}

Know the answer?
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
Similar Questions