分类目录归档:Code

COM6506

Week 02

public class Person {
    String name;
    double weight;
    double height;

    public Person(String name, double weight, double height) {
        this.name = name;
        this.weight = weight;
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public double getWeight() {
        return weight;
    }

    public double getHeight() {
        return height;
    }

    public static void main(String[] args) {
        Person jeff = new Person("Jeff", 72.4, 2.2);
        Person jim = new Person("Jim", 65, 1.7);
        System.out.println("Jeff is " + jeff.getHeight() + "m tall.");
        System.out.println("Jim is " + jim.getHeight() + "m tall.");
    }
}

继续阅读

COM6516

Week01-02

/*
 * HelloWorld.java
 * A traditional Hello World program!
 */

// In Java, all programs reside in a class. This class is called HelloWorld, 
// and should be saved in a file called HelloWorld.java. This class has a single
// method called main, which is the entry point to the program.
//
// Compiling the class with javac HelloWorld.java will produce a bytecode file
// called HelloWorld.class if compilation is successful. This bytecode can then
// be run on any machine with a java bytecode interpreter. You can run the
// bytecode in a console by typing java HelloWorld.

public class HelloWorld {
    public static void main(String[] arg) {

        String helloString = "Hello";
        String worldString = "World!";

        // In Java the System.out.println command displays the argument to the 
        // console. However the command below doesn't work, because helloWorldAString
        // has not been declared. Try compiling this file to see what happens, 
        // and take a careful look at the error message that is produced.

        System.out.println(helloString + " " + worldString);

        // In Java, we can use the '+' operator to concatenate strings, 
        // so to fix this problem, either change the argument passed to the
        // System.out.println method from (helloWorldString) to 
        // (helloString + " " + worldString)
        // or declare the variable helloWorldString before it is used by 
        // System.out.println by inserting 
        // String helloWorldString = helloString + " " + worldString;

        // In Java a variable can be declared anywhere in the code, so it is
        // possible to declare a variable just before it is used, which makes for
        // code that is easier to read and understand.

        // It is conventional to use mixed case for variable names and method 
        // names in Java, with with the first letter lower case, and then the 
        // first letter of each internal word in upper case -- e.g. helloString.
        // Class names start with a capital letter -- e.g. HelloWorld.java

        // details at: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html

    }
}

继续阅读

Python 程序设计

第二周

  • 利用嵌套循环 , 输出 2-100 之间的质数。
  • 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
  • 输出 9*9 乘法口诀表。
# 输出2-100之间的素数
for i in range(2, 101):
    for j in range(2, i):
        if not (i % j):         # 如果 i 整除 j(i 不是素数)
            break                   # 跳出循环
    else:
        print(i, "是素数")

继续阅读

互联网应用 Lab

Lab 3

#include <unistd.h>
#include <stdio.h>

int main() {
    char *arg[] = {"/bin/ls", 0};

    /* fork, and exec within child process */
    if (fork() == 0) {
        printf("In child process:\n");
        execv(arg[0], arg);
        printf("I will never be called\n");
    }

    printf("Execution continues in parent process\n");

    return 0;
}

继续阅读

Java Lab

Lab 1

新学期,新的语言学习开始了~还是老样子,写代码~

public class TestArgs {
    public static void main (String[] args) {
        System.out.println("Name            = " + args[0] + " " + args[1]);
        System.out.println("BUPT Email Username = " + args[2]);
        System.out.println("Student Number      = " + args[3]);
    }
}

继续阅读

Coursework 2015

Question

Write a program that will help elementary school pupils practice math.

a) The program will first ask the user for his/her ID number (including two letters & 4 digits), e.g.:

Please input your four digit ID no: AB1234
The program should have input validation.
Then the program prompts three choices:
* Start a test
* Check scores
* Exit
继续阅读