联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2019-03-29 10:40

2019S CS102A Assignment 2


A2Q1: Chinese Lunar year[20 points]

Please write a program to show the Chinese Lunar year of Stems-and-Branches(天干地支) according to the year number, and the animal symbol(属相) of the year which a person was born in.

The Stems-and-Branches uses 10 Stems called:jia, yi, bing, ding, wu, ji, geng, xin, ren, and gui(甲乙丙丁戊己庚辛壬癸), and 12 Branches called zi, chou, yin, mao, chen, si, wu, wei, shen, you, xu and hai(子丑寅卯辰巳午未申酉戌亥).Combining each of the10 Stems with one of the 12 Branches in sequence creates 60 chronological symbols.For example jiazi, yichou, bingyin, etc. These 60 symbols are used in circles and thus each year has a chronological symbol. And the Branches also represents the animal symbol of Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig. For example, it is the year of JiHai(己亥年) in 2019. And it is the year of Pig.


Search on web how to calculate it. And write a program using switch statement.

Tips:

(1)Remainder=Year%10, {(Stems:remainder) }={(jia:4)}{(yi:5)}(bing:6)(ding:7)(wu:8)(ji:9)(geng:0)(xin:1)(ren:2)(gui:3)

(2)Remainder=Year%12

{(Branches:remainder) }={(zi:4)}{(chou:5)}{(yin:6)}{(mao:7)}{(chen:8)}{(si:9)}{(wu:10)}{(wei:11)}{(shen:0)}{(you:1)}{(xu:2)}{(hai:3)}

(3)Branches and animal pair

{(zi:Rat)}{(chou:Ox)}{(yin:Tigger)}{(mao:Rabbit)}{(chen:Dragon)}{(si:Snake)}{(wu:Horse)}{(wei:Sheep)}{(shen:Monkey)}{(you:Rooster)}{(xu:Dog)}{(hai:Pig)}


ref: http://baijiahao.baidu.com/s?id=1543800345816500


Sample output:


Sample code:

public class A2Q1 {

   public static void main(String[] args){

       int year = Integer.parseInt(args[0]);

//define the array of Stems-and-Branches, animal symbols

       String stem[] = ;

       String branch[] = ;

       String animal[] = ;

// to calculate

// output

       System.out.printf("%s is the year of %s-%s. Also %s year.",...);

   }

}


A2Q2: Simple Statistic of number sequence  [30 points]

Write a program to calculate the average(平均数), mode(众数) and median(中位数) of a sequence of numbers.

The average of the numbers is the sum divided by count. The mode is the number appears most frequently. There may be more than 1 mode in a number sequence. The median is the number at “Middle” position of the sequence after sorting. The median is at the “Middle” position of (n-1)/2+1 when n is odd, or  is the average value of numbers at position of n/2 and n/2+1.


Using Array and for statement.

(1)Calculate the average;

(2)Count the numbers and find the number appears max times;

(3)Sort all numbers from small to big, and find the “Middle”;


Tips: args.length shows how many parameters inputted.


Sample output:


Sample code:

public class A2Q2 {

   public static void main(String[] args){

       double number[];

       double average = 0.0;

       double mode = 0.0;

       double median = 0.0;

       

       number = new double[args.length];

       //1. to get the input numbers and store in array


       for (int i=0; i<args.length; i++){

           

       }

       //2. average

       double sum=0.0;

     

       System.out.printf("average = %.2f\n",average);

       //3. count numbers

       int count[] = new int[args.length];

       for (int i=0; i<args.length; i++){

           count[i]=1;

       }

       for (int i=0; i<args.length; i++){

           

       }

       int maxCount=0;

//3.1 find the max count

       for (int i=0; i<args.length; i++){

           

       }

//3.2 find the mode with max count

       System.out.print("mode = ");

       

       //4.1 sorting

       for (int i=0; i<args.length; i++){

           for (int j=0; j<args.length; j++){

           

           }

       }

       

// 4.2 median



       System.out.printf("\nmedian = %.2f",median);

   }

}





A2Q3: Grading system [20 points]

Please write 2 programs to calculate the GPA at SUSTech:


(1)A2Q3_1:Input a score of letter grading, and to calculate the Grade Point.

Tips: string.CharAt(int index) returns the value of the character at the specified index of the string


(2)A2Q3_2:Input a serials of percentage grading and credit hour, and to calculate the GPA.


Sample output:


Sample code:

public class A2Q3_2 {

   public static void main(String[] args){

       double score[];

       int credit[];


       if (0==args.length || 0 != args.length%2){

           System.out.println("Please input the right format of score and credit hour in pair, eg. 95 2 88 3");

           return;

       }


       score = new double[args.length/2];

       credit = new int[args.length/2];

       // to get the input numbers and store in array


       for (int i=0; i<args.length; i+=2){

           

       }


       // calculate GP

       double GP[] = new double[args.length/2];

       for (int i=0; i<args.length/2; i++){

           

       }

       double GPA=0.0;

     

       System.out.printf("GPA = %.2f",GPA);

   }

}



Question 4: Calculate [30 points]

Write a program using basic arrays to  calculate the value of an arithmetic expression consisting of numbers( could be floating-point) and operators +,-,*and / .  The arithmetic expression is passed to the program from the command line as a string.

Tips:

(1)We can assume that the input string always represents a valid arithmetic expression;

(2)Only 3 numbers and 2 operators;maybe with a pair of bracket;


Sample output:


Sample code:

public class A2Q4 {

   public static void main(String[] args) {

       //if (0 == args.length) return;

       int n = args[0].length();

       double num[] = new double[3];//

       char cal[] = new char[2];//

       int nums = 0;

       int cals = 0;

       String strNumber = "";

       //to parse the string

       for (int i = 0; i < n; i++) {

           //System.out.print(args[0].charAt(i));

           switch (args[0].charAt(i)) {

               case '+':

                   // to do youself

                   break;

               case '-':

                   // to do youself

                   break;

               case '*':

                   // to do youself

                   break;

               case '/':

                   // to do youself

                   break;

               default:

                   // to do youself

                   break;

           }

           //System.out.print(nums+","+cals);

       }

       

       // to calculate

       double result = 0.0;

       if (('+' == cal[0] || '-' == cal[0]) && ('*' == cal[1] || '/' == cal[1])){

// to finish

}

else

{

// to finish

}

// to print result

       System.out.printf("%s=%.2f", args[0], result);

   }

}


Rules

1.Please submit “.java” file of these five questions.

2.The class name of each “.java” file should be A2Q1, A2Q2, A2Q3_1,A2Q3_2 , A2Q4 respectively to represent these five questions.

3.No Chinese characters are allowed to appear in your code.

4.No package included.

5.The arguments and the output must strictly follow the description of each question.

6.Please submit your assignment on the SAKAI site of your lab section. Marks will be deducted if you submit later than the deadline. If you submit your assignment within 24 hours after the deadline (grace period), your score will be half of the score you could get if the submission was made before the deadline. Assignments submitted after the grace period will not be graded (meaning you will get a zero for the assignment).


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp