Question

Please help me do the java project For this project you will be reading in a...

Please help me do the java project

For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file:

students.txt

ID              Name                              Age                    IsMale           GPA

1                Tom Ryan                       22                       True              3.1

2                Jack Peterson                31                       True              2.7

3                Cindy LuWho                12                       False             3.9

When you read in the header line, you will need to create the properties to go along with these. The problem is what datatype to use for each… In order to do this you will also need to read the data that goes along with each field, determine what datatype you need to set for the filed, and then create the property. The example main() code on the following page will give help you to do this. I’ve created the methods for you to use in your code.

I would suggest you create yourself a class called Property with, at least, two String properties. You may want to add some methods in the class as well to make things easier when creating the output but it is not necessary:

public class Property {

       public String fieldName;

       public String dataType;

}

So, once you have determined each field name and datatype you will need to create a Class with properties, constructors, methods, and getters/setters. The below is, if done correctly, the auto created class file called Students.java

public class Students {

       //====================================== Properties

private int iD;

private String name;

private int age;

private boolean isMale;

private double gPA;

       //====================================== Constructors

       public Student(int iD, String name, int age, boolean isMale, double gPA) {

              setID(iD);

              setName(name);

              setAge(age);

              setIsMale(isMale);

              setGPA(gPA);

       }

       public Student(Scanner fin) throws Exception {

              String[] parts = fin.nextLine().split("\t");

              setID(Integer.parseInt(parts[0]);

              setName(parts[1]);

              setAge(Integer.parseInt(parts[2]);

              setIsMale(Boolean.parseBoolean(parts[3]));

              setGPA(Double.parseDouble(parts[4]));

       }

       //====================================== Methods

       public boolean equals(Object obj) {

              if(!(obj instanceof Student)) return false;

              Student s = (Student)obj;

              return getEqualsString().equals(s.getEqualsString());

       }

private String getEqulsString() {

       return iD + "~" + name + "~" + age + "~" + isMale + "~" + gPA;

}

       public String toString() {

              return "ID: " + iD + ", Name: " + name + ", Age: " + age + ", IsMale: " + isMale + ", GPA: " + gPA;

       }

       //====================================== Getters/Setters

       ... you know what to do here

Sample code for checking datatypes

public static void main(String[] args) {

            String[] data = {"1", "Tom Ryan", "22", "True", "3.1"};    

            for(String s : data) {

                  System.out.println(s + " isInt: " + isInt(s));

                  System.out.println(s + " isLong: " + isLong(s));

                  System.out.println(s + " isFloat: " + isFloat(s));

                  System.out.println(s + " isBoolean: " + isBoolean(s));

                  System.out.println("======================================");

            }

      }

      // Methods for checking for datatype o

      public static boolean isInt(String val) {

            try {

                  Integer.parseInt(val);

                  return true;

            } catch (Exception e) { return false; }

      }

     

      public static boolean isLong(String val) {

            try {

                  Long.parseLong(val);

                  return true;

            } catch (Exception e) { return false; }

      }

           

      public static boolean isFloat(String val) {

            try {

                  if(val.contains(".")) {

                        Double.parseDouble(val);

                        return true;

                  }

                  return false;

            } catch (Exception e) { return false; }

           

      }

     

      public static boolean isBoolean(String val) {

            return val.equalsIgnoreCase("True") || val.equalsIgnoreCase("False");        

      }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Use this java code It may help you

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Students {

    //====================================== Properties

    private int iD;

    private String name;

    private int age;

    private boolean isMale;

    private double gPA;

    //====================================== Constructors

    public Students(int iD, String name, int age, boolean isMale, double gPA) {

        setID(iD);

        setName(name);

        setAge(age);

        setIsMale(isMale);

        setGPA(gPA);

    }

    public Students(Scanner fin) throws Exception {

        String line  = fin.nextLine();
        Scanner words = new Scanner(line);
       /* String[] parts = fin.nextLine().split("\t");
        if(isInt(parts[0]))
        setID(Integer.parseInt(parts[0]));
        setName(parts[1]);
        if(isInt(parts[2]))
        setAge(Integer.parseInt(parts[2]));
        if(isBoolean(parts[3]))
        setIsMale(Boolean.parseBoolean(parts[3]));
        if(isFloat(parts[4]))
        setGPA(Double.parseDouble(parts[4]));*/
       setID(words.nextInt());
       setName(words.next()+" "+words.next());
       setAge(words.nextInt());
       setIsMale(words.nextBoolean());
       setGPA(words.nextDouble());

    }
    //setters

    public void setID(int iD) {
        this.iD = iD;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setIsMale(boolean male) {
        isMale = male;
    }

    public void setGPA(double gPA) {
        this.gPA = gPA;
    }


    //====================================== Methods

    public boolean equals(Object obj) {

        if(!(obj instanceof Students)) return false;

        Students s = (Students)obj;

        return getEqualsString().equals(s.getEqualsString());

    }

    private String getEqualsString() {

        return iD + "~" + name + "~" + age + "~" + isMale + "~" + gPA;

    }

    public String toString() {

        return "ID: " + iD + ", Name: " + name + ", Age: " + age + ", IsMale: " + isMale + ", GPA: " + gPA;

    }

    //====================================== Getters/Setters

      /* ... you know what to do here

    Sample code for checking datatypes*/

    public static void main(String[] args) {
/*
        String[] data = {"1", "Tom Ryan", "22", "True", "3.1"};

        for(String s : data) {

            System.out.println(s + " isInt: " + isInt(s));

            System.out.println(s + " isLong: " + isLong(s));

            System.out.println(s + " isFloat: " + isFloat(s));

            System.out.println(s + " isBoolean: " + isBoolean(s));

            System.out.println("======================================");

 */

            try {
                Scanner scanFile = new Scanner(new File("students.txt"));
                //read header
                String header = scanFile.nextLine();
                
                while (scanFile.hasNextLine()) {
                    Students students = new Students(scanFile);
                    //print student data
                    System.out.println(students);
                }
            }
            catch (FileNotFoundException e)
            {
                System.err.println("File not found...");
            } catch (Exception e) {
                e.printStackTrace();
            }

    }



    // Methods for checking for datatype o

    public static boolean isInt(String val) {

        try {

            Integer.parseInt(val);

            return true;

        } catch (Exception e) { return false; }

    }



    public static boolean isLong(String val) {

        try {

            Long.parseLong(val);

            return true;

        } catch (Exception e) { return false; }

    }



    public static boolean isFloat(String val) {

        try {

            if(val.contains(".")) {

                Double.parseDouble(val);

                return true;

            }

            return false;

        } catch (Exception e) { return false; }



    }



    public static boolean isBoolean(String val) {

        return val.equalsIgnoreCase("True") || val.equalsIgnoreCase("False");

    }
}

//File

//Output

//If you need any help regarding this solution ........... please leave a comment ......... thanks

Add a comment
Know the answer?
Add Answer to:
Please help me do the java project For this project you will be reading in a...
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
  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...

  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student {    private String firstName;    private String lastName;    private String id;    private boolean tuitionPaid;       public Student(String firstName, String lastName, String id, boolean tuitionPaid)    {        this.firstName=firstName;        this.lastName=lastName;        this.id=id;        this.tuitionPaid=tuitionPaid;    }   ...

  • Write a unit test to test the following class. Using Java : //Test only the debit...

    Write a unit test to test the following class. Using Java : //Test only the debit method in the BankAccount. : import java.util.*; public class BankAccount { private String customerName; private double balance; private boolean frozen = false; private BankAccount() { } public BankAccount(String Name, double balance) { customerName = Name; this.balance = balance; } public String getCustomerName() { return customerName; } public double getBalance() { return balance; } public void setDebit(double amount) throws Exception { if (frozen) { throw...

  • Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters...

    Modify the objectstudent JAVA program to add a private variable zipcode, you must also add setters and getter methods for the new variable and modify the toString method. The objectstudent program is in this weeks module, you can give it a default value of 19090. class Student { private int id; private String name; public Student() { id = 8; name = "John"; } public int getid() { return id; } public String getname() { return name; } public void...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

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