22 May, 2020

Test of Association Assignment


Name________________________________
This assignment is worth 100 points. Each problem/part is worth 5 points each.
Due Date: ThursdayMay 21, 2020 by 11:59p.m. EST.
You do not have to type your responses. If you wish to provide hand-written work and scan your document, that is fine. Upload your final document with complete responses to the ‘Test of Association Assignment’ in Canvas.

1.       Some students in the masters of analytics program at Harrisburg University claim they are really good at soccer. To see whether there is an association among students and faculty in terms of the goals they can score in a soccer game, the analytics faculty invited students and teaching assistants (Ph.D. students) to play in soccer matches.  Every player was allowed to play in only one match. Over many matches, we counted the number of players who scored goals.

a.       The soccer.txt file is attached to the ‘Test of Association Assignment’ in Canvas. Import this file into R. Copy and paste your code below that shows where you imported the data. Name the data frame that you create ‘soccer.’
library(readr)
soccer <- read_csv("kochu/soccer.txt")        
b.      When you examine the data frame you imported into R, you will notice that it is not in the appropriate format for performing a chi-square test. Using some of the R categorical functions that you have learned in the course, convert the soccer data frame that you imported in part a in to a table such that the Job variable is on the rows and the Score variable is on the columns. Name the table you create ‘soccer_t’. Copy and paste your code below that created the table.
soccer_t <- read.delim("soccer.txt")
Table create below

c.       Develop the appropriate null and alternative hypothesis for testing an association among these variables. State the null and alternative hypothesis in the context of this problem.

H0: There is no relationship between occupation with the frequency of scoring in a soccer match.
H1: There is a relationship between occupation with the frequency of scoring in a soccer match.


d.      Run the chi-square test in R using the assocstats() function. Copy and paste your code and the output below. What is the value of the chi-square test statistic? What is the p-value related to the chi-square test statistic?
tab<- table(job,freq)
summary(assocstats(tab))



Chi-square test statistic is 12
The p-value is 0.2851
e.      Compute the χ2 test statistic that you obtained from the R output in part d.  You should show your computations. Show the computations of the expected counts. Provide a final table that includes the observed counts with the expected counts in parenthesis for each cell. The final table should have row and column margin totals and a grand total.
summary(chisq.test(tab))


f.        Does the test statistic you found by hand in part e match the χ2 test statistic from the assocstats() output in part d? State Yes or No.

No


g.       Use the pchisq() function in R to find the p-value associated with the χ2 test statistic. Copy and paste your code below.  Does the p-value you found with the pchisq() function match the p-value from the assocstats() output in part d? State Yes or No.
YES
1-pchisq(12, 10, ncp = 0, lower.tail = TRUE, log.p = FALSE)
P value is 0.2850565 to four decimal places is 0.2851 
hence it is similar to the p-value using assocstats ()


h.      Using the p-value for this test, do you reject or not reject the null hypothesis?

Accept the null hypothesis


i.        State a conclusion back in terms of the context of the problem.

The null hypothesis is accepted because the p-value is greater than 0.05.









12 March, 2020

Final Research

Write a brief research report (up to about 7 pages, not including title page, abstract, and references), based on an analysis of the data file. Choose a hypothesis, cite at least three references to justify your hypothesis, test your hypothesis with an analysis of the DATA540.SAV file, and then report and discuss the results. Your results should include both descriptive and inferential statistics. 
 
Use APA format, including an introduction, abstract, method, results, and discussion section. Please refer to the APA manual: American Psychological Association   (2010).   Publication manual of the American Psychological Association.   (6th ed.).   Washington, D.C.

26 January, 2020

Lab 4 - Mutability



Lecturer:

Report Title:

Submit to:

Deadline for submission:


Student Name:

Student Number:

Programme of Study:

Module:










Contents

 



Description

An immutable project can be defined as a program that cannot be changed once a variable has been instantiated. The program contains attributes that makes it impossible to be changed once it has been created (Kjolstad, Dig, Acevedo, & Snir, 2010). The project contains protective measures that make it impossible to add or remove objects once it has been created. Immutable program contains different objects that work together to ensure that the program is not destroyed once it has been created. The project involves testing an existing class known as Planet.java to see whether it has attributes that makes it an immutable program. The test is conducted by reading the code and establishing objects that makes it immutable and also developing a test.java class to test the planet. Java. The test class should be able to use the existing methods in the class planet and establish whether or not the class is immutable.

Aims

       I.            To determine whether the program is immutable
    II.            Develop a strategy that can be used to define immutable objects
 III.            To study and understand the Date object
 IV.            To develop a driver class for the project

Method

The methodology used to analyze the program to determine whether it is immutable is conducted using NetBeans IDE.  
Determining whether the program is immutable
The code below shows the declaration of the code as public final class Planet; this is because no children classes should be created from the immutable class Planet.
public final class Planet
{
}
The fragment of code from the program below shows string name and Date discoveryDate objects. It is an immutable program because the constructor public Planet has parameters.  The parameterized constructor makes it impossible for the project to be changed after it has been created.
public Planet (String name, Date discoveryDate)
{
     this.name = name;
this.discoveryDate = new Date (discoveryDate.getTime ());
  }
The object string name is immutable because once it is created it cannot be changed anywhere in the program.
Both the variables name and discoveryDate have get methods have seen below for each one of them.
public String getName()
  {
    return name;
  }
The public String getName() return name as it is evident in the fragment of code above.


  public Date getDiscoveryDate()
  {
    return new Date(discoveryDate.getTime());
  }
The code above public Date getDiscoveryDate() returns new Date().
The program does not have setter methods to enable change of the variables in the program once it has been created.
The tester code below proves that the class is immutable
// Driver class

import java.util.*;

class Test
{
    public static void main(String args[])
    {
        Date date = new Date();
        Planet p;
        p = new Planet("Testing the Planet Class", date);
        System.out.println(p.getName());
        System.out.println(p.getDiscoveryDate());
 
       
    }
}
The tester code shows that the variables defined in the class planet cannot be changed and it has to be re-used. As it is evident on the code above the class Planet has to be called so as to be able to access the variables name and discoveryDate using the get methods available in class Planet.
Strategy to define immutable objects
The best strategy to define immutable objects is by first opening a program using different java IDE. Once the program has been opened reading and understanding the code is important in understanding the type of program and objects. The objects is plays a critical part in determining the immutable nature of a program. The program should have a constructor that has parameters, there should be a get method for all the variables in the program and finally the program should not have a set method for any of the variables.
Understanding the date variable
The date variable when it comes to immutable objects is complex. This is because the date belongs to data type Date. It is evident in our test class above as we can see in the code below:-
Date date = new Date();
The object date had to be initiated using the function new Date (). The double initiation of date object makes it complex to use. Date object is discouraged from use because it has major flows since it was developed. Such flows includes it lacks time zone, the world contains different time zones. The lack of time zones in the java.util.Date makes it lack necessary features that is important to the users of the systems developed using the object. It also lacks time format that is necessary in ensuring that users of the system understand the time being displayed (Nikolic, 2017). The weakness makes it harder because the software engineer has to design a time format for the program which is time consuming. It also lacks a calendar system. The calendar system has to be developed hence adding work to the individual or team using java util Date for the function of software development. This is not flexible because it rates years based on the year 1900 and it results in problems when using years prior to 1900 (Nikolic, 2017).

Results and Testing

It displayed the string “Testing the Planet Class”, the string was defined with the string posted in the test class. The date displayed was the current time defined on the test class but it was displayed using the function getDiscoveryDate from the Planet class. The same applies to the string name that was from class Planet.

Conclusions

In conclusion, the aims of the study has been met, this because the program has been confirmed to be immutable, the object date has been studying and understood perfectly,  the test class for the main program was developed and finally the strategy to define an object based on immutable program developed. The project is immutable based on (Haack, Poll, Schafer, & Schubert, 2017) definition of an immutable program. The program does not have setter methods that can be used to change the program after it has been instantiated. The program only contains constructors and getter methods that cannot be used to change the program variables once they have been instantiated.

References

 

Haack, C., Poll, E., Schafer, J., & Schubert, A. (2017). Immutable Objects for a Java-like
Language. German research foundation, Retrieved from https://www.cs.ru.nl/E.Poll/papers/esop07.pdf .
Kjolstad, F. B., Dig, D., Acevedo, G., & Snir, M. (2010). Refactoring for Immutability.
Making Classes Immutable, Retrieved from https://core.ac.uk/download/pdf/4824892.pdf .
Nikolic, D. (2017). Still using java.util.Date? Don’t! Programming Hints, Retrieved from
 https://programminghints.com/2017/05/still-using-java-util-date-dont/ .


Appendices

2020-01-25 22_07_41-JavaApplication5 - NetBeans IDE 8.0.2.png

Figure 1: Class Planet
test.png
Figure 2:Class Test


Lab 4 - Mutability


Lab 4 - Mutability

“Immutable objects are simple. They can only be in one state, which is carefully controlled by the constructor. One of the most difficult elements of program design is reasoning about the possible states of complex objects. Reasoning about the state of immutable objects, on the other hand, is trivial.

Immutable objects are also safer. Passing a mutable object to untrusted code, or otherwise publishing it where untrusted code could find it, is dangerous — the untrusted code might modify its state, or, worse, retain a reference to it and modify its state later from another thread. On the other hand, immutable objects cannot be subverted in this manner by malicious or buggy code, so they are safe to share and publish freely without the need to make defensive copies.” 
Brian Goetz, Java Concurrency in Practice

Introduction

An immutable object is one whose externally visible state cannot change after it is instantiated. The String, Integer, and BigDecimal classes in the Java class library are examples of immutable objects -- they represent a single value that cannot change over the lifetime of the object.

Lab Instructions:

Consider the following code:
import java.util.Date;

public final class Planet {

  String name;
  privatefinalDate discoveryDate;

  public Planet (String name, Date discoveryDate) {
     this.name = name;
this.discoveryDate = new Date(discoveryDate.getTime());
  }


  public String getName() {
    return name;
  }


  public Date getDiscoveryDate() {
    return new Date(discoveryDate.getTime());
  }



}




1.      Is this program immutable? If you think it is not, what changes would need to be implemented and why? If you think it is, explain why? To explain you need to create a tester class to run the code and see if you can change any values.
2.      Did you notice anything about the Date object? The Date object is no longer used in the latest versions of Java. Research why this is so and how it relates to Mutable Code.
3.      Using what you have learned about Mutability. Detail a strategy that could be used for defining immutable objects.
Remember to write your conclusions on your work.

Submission and Grading Details

This lab will form part of your complete Lab book, which must be submitted at the end of the module. You must reference your work.  You should follow the layout of a typical lab book, and add extra headings as necessary. Sample layout below:
·         Lab3 Mutability
o   Description
o   Aims
o   Method  
o   Results
§  Section 1 What is Mutability and Immutability
§  Section 2 Immutable Code or Mutable Code?
§  Section 3 Deprecated Object ‘Date’
§  Section 4 A strategy for defining immutable objects
o   Conclusions
o   Appendix
o   References 



Marks Available
Description
5
Aims
5
Method
5
Results/Testing/Evaluation
10
Is code Mutability or Not, explain
15
Deprecated Object ‘Date’
10
Outline a Strategy that could be used
10
Conclusion
40


TOTAL
100