first commit

master
王宇洋 3 years ago
parent 8d78442a47
commit 5fd94da12b

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Methmology.iml" filepath="$PROJECT_DIR$/Methmology.iml" />
</modules>
</component>
</project>

@ -0,0 +1,3 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

File diff suppressed because one or more lines are too long

@ -0,0 +1,177 @@
# Software Methodology 11.15
## WangYuyang 19372316
## Design Pattern
The design pattern I chose is the **factory pattern**. First, in this program the user wants to create a grading algorithm, as long as he knows its name, and does not need to care about its specific implementation. Secondly, the extensibility is high, if needed to add a new grading algorithm, just change the factory class. At last, Shielding the specific implementation of the product, the caller only needs to care about the interface of the product
## Class Diagram
![class-diagram](class-diagram.png)
## Code
### GradingScheme.java
```java
public interface GradingScheme {
public double getGrade(double assignment, double exam);
}
```
### GradingSchemeFactory.java
```java
public class GradingSchemeFactory {
public static enum Scheme {
TYPE_A,
TYPE_B
}
public GradingScheme getGradingScheme(Scheme scheme){
if(scheme == Scheme.TYPE_A){
return new MarkingAlgorithm(0.4,0.6);
}
else if(scheme == Scheme.TYPE_B){
return new MarkingAlgorithm(0.5,0.5);
}
throw new RuntimeException("Unknown scheme");
}
}
```
### MarkingAlgorithm.java
```java
public class MarkingAlgorithm implements GradingScheme {
private double assignmentContribution;
private double examContribution;
public MarkingAlgorithm(double assignmentContribution, double examContribution) {
if ((assignmentContribution < 0 || assignmentContribution > 1)
|| (examContribution < 0 || examContribution > 1)) {
throw new RuntimeException(
"The assignment contribution and " +
"exam contribution should be " +
"between 0 and 1, now: " +
assignmentContribution +
" " +
examContribution);
}
if ((assignmentContribution + examContribution) != 1) {
throw new RuntimeException("The sum of assignment contribution and " +
"exam contribution should be " +
"1, now: " +
(assignmentContribution + examContribution));
}
this.assignmentContribution = assignmentContribution;
this.examContribution = examContribution;
}
@Override
public double getGrade(double assignment, double exam) {
return assignmentContribution * assignment + examContribution * exam;
}
}
```
### Student.java
```java
public class Student {
private String name; // full name of the student
private Double assignment; // score for the assignment
private Double exam; // score for the exam
private GradingScheme gradingScheme;
/**
* Construct the student from their name
* @param name full name of the student
*/
public Student(String name) {
this.name = name;
assignment = 0.0;
exam = 0.0;
}
/**
* @return the student's full name
*/
public String getName() {
return name;
}
/**
* @param as the assignment score to set
*/
public void setAssignmentScore(double as) {
assignment = as;
}
/**
* @param es the exam score to set
*/
public void setExamScore(double es) {
exam = es;
}
// TODO get the final score
public Double getFinalScore() {
if(gradingScheme == null) {
throw new RuntimeException("Grading scheme not set");
}
return gradingScheme.getGrade(assignment, exam);
}
public void setGradingScheme(GradingScheme gradingScheme) {
this.gradingScheme = gradingScheme;
}
}
```
### TestCase.java
```java
public class TestCase {
public static void main(String[] args) {
Student student = new Student("Harry Potter");
student.setAssignmentScore(88);
student.setExamScore(66);
GradingSchemeFactory factory = new GradingSchemeFactory();
// TODO switch to algorithm A:
student.setGradingScheme(
factory.getGradingScheme(
GradingSchemeFactory.Scheme.TYPE_A
));
System.out.println(
student.getName()
+ " "
+ student.getFinalScore().toString());
// TODO switch to algorithm B
student.setGradingScheme(
factory.getGradingScheme(
GradingSchemeFactory.Scheme.TYPE_B
));
System.out.println(
student.getName()
+ " "
+ student.getFinalScore().toString());
}
}
```
## Output
```shell
Harry Potter 74.80000000000001
Harry Potter 77.0
```

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 86 KiB

@ -0,0 +1,12 @@
package com.echo;
/**
* @Author: WangYuyang
* @Date: 2021/11/15-14:56
* @Project: Methodology
* @Package: com.echo
* @Description:
**/
public interface GradingScheme {
public double getGrade(double assignment, double exam);
}

@ -0,0 +1,24 @@
package com.echo;
/**
* @Author: WangYuyang
* @Date: 2021/11/15-15:20
* @Project: Methmology
* @Package: com.echo
* @Description:
**/
public class GradingSchemeFactory {
public static enum Scheme {
TYPE_A,
TYPE_B
}
public GradingScheme getGradingScheme(Scheme scheme){
if(scheme == Scheme.TYPE_A){
return new MarkingAlgorithm(0.4,0.6);
}
else if(scheme == Scheme.TYPE_B){
return new MarkingAlgorithm(0.5,0.5);
}
throw new RuntimeException("Unknown scheme");
}
}

@ -0,0 +1,39 @@
package com.echo;
/**
* @Author: WangYuyang
* @Date: 2021/11/15-14:58
* @Project: Methodology
* @Package: com.echo
* @Description:
**/
public class MarkingAlgorithm implements GradingScheme {
private double assignmentContribution;
private double examContribution;
public MarkingAlgorithm(double assignmentContribution, double examContribution) {
if ((assignmentContribution < 0 || assignmentContribution > 1)
|| (examContribution < 0 || examContribution > 1)) {
throw new RuntimeException(
"The assignment contribution and " +
"exam contribution should be " +
"between 0 and 1, now: " +
assignmentContribution +
" " +
examContribution);
}
if ((assignmentContribution + examContribution) != 1) {
throw new RuntimeException("The sum of assignment contribution and " +
"exam contribution should be " +
"1, now: " +
(assignmentContribution + examContribution));
}
this.assignmentContribution = assignmentContribution;
this.examContribution = examContribution;
}
@Override
public double getGrade(double assignment, double exam) {
return assignmentContribution * assignment + examContribution * exam;
}
}

@ -0,0 +1,51 @@
package com.echo;
public class Student {
private String name; // full name of the student
private Double assignment; // score for the assignment
private Double exam; // score for the exam
private GradingScheme gradingScheme;
/**
* Construct the student from their name
* @param name full name of the student
*/
public Student(String name) {
this.name = name;
assignment = 0.0;
exam = 0.0;
}
/**
* @return the student's full name
*/
public String getName() {
return name;
}
/**
* @param as the assignment score to set
*/
public void setAssignmentScore(double as) {
assignment = as;
}
/**
* @param es the exam score to set
*/
public void setExamScore(double es) {
exam = es;
}
// TODO get the final score
public Double getFinalScore() {
if(gradingScheme == null) {
throw new RuntimeException("Grading scheme not set");
}
return gradingScheme.getGrade(assignment, exam);
}
public void setGradingScheme(GradingScheme gradingScheme) {
this.gradingScheme = gradingScheme;
}
}

@ -0,0 +1,34 @@
package com.echo;
public class TestCase {
public static void main(String[] args) {
Student student = new Student("Harry Potter");
student.setAssignmentScore(88);
student.setExamScore(66);
GradingSchemeFactory factory = new GradingSchemeFactory();
// TODO switch to algorithm A:
student.setGradingScheme(
factory.getGradingScheme(
GradingSchemeFactory.Scheme.TYPE_A
));
System.out.println(
student.getName()
+ " "
+ student.getFinalScore().toString());
// TODO switch to algorithm B
student.setGradingScheme(
factory.getGradingScheme(
GradingSchemeFactory.Scheme.TYPE_B
));
System.out.println(
student.getName()
+ " "
+ student.getFinalScore().toString());
}
}
Loading…
Cancel
Save