Java programming exercises

Programming exercise:

1. Create thread classes Cat and Dog by implementing the Runnable interface. The function of the run() method is to add a for loop with a loop length of 3 and output the information "A cat" and "A dog" respectively.

2. Create objects of Cat and Dog classes in the test class, and start two threads.

3. Create a for loop with a loop length of 3 in the test class, and print out the information "main thread".

Test class

public class Test {

    public static void main(String[] args) {
        // Method stubs automatically generated by TODO
        for(int i=0;i<3;i++) {
            System.out.println("main thread");
        }
        Cat one=new Cat();
        Dog two=new Dog();
        Thread t1=new Thread(one);
        Thread t2=new Thread(two);
        t1.start();
        t2.start();
//        new Thread(t1).start();
//        new Thread(t2).start();
    }
}

Cat class

class Cat implements Runnable {
	public void run() {
		for (int i=0;i<3; i++) {
			System.out.println(Thread.currentThread().getName() + "A cat");
		}
 
	}
}
 

Dog class

class Dog implements Runnable {
	public void run() {
		for (int i=0;i<3; i++) {
			System.out.println(Thread.currentThread().getName() + "A dog");
		}
 
	}
}

 

2. (single choice question)

The main tasks of creating threads with the Runnable interface are as follows. Their correct order is ()

(1) create thread class objects by implementing class objects

(2) declare classes that implement the Runnable interface

(3) call the start() method to start the thread

(4) create objects that implement classes

(5) implement the run() method in the implementation class

Correct answer: c my answer: C

  • A. 

    1-4-2-5-3

  • B. 

    2-1-4-5-3

  • C. 

    2-5-4-1-3

  • D. 

    1-5-2-4-3

thread

//Process can run multiple program blocks at the same time to achieve the purpose of multi task processing
//Activate multiple threads at the same time
/*To enable a class to activate a thread, use the following syntax:
* class class name extends Thread / / extends subclasses from the thread class
 * {
* properties
* method
* modifier run() {/ / overrides the run() method in the Thread class
* threaded programs
 */

3. programming exercise:

Create a Thread by inheriting the Thread class, and print out the contents as shown in the demonstration effect through a loop in the Thread body.

public class Test {
       public static void main(String[] args) {
           new TestThread().start();      
       }
}
class TestThread extends Thread{
    public void run() {
        for(int i=1;i<=10;i++) {
            System.out.println("printer is running" +i);
        }
    }
}

 4. In the following code, what statement is added at the dash to successfully start the thread.

Answer: one start();

5. (multiple choice) which two sentences are wrong? (select two)

Correct answer: bc my answer: BC

  • A. 

    A thread is a running unit smaller than a process

  • B. 

    The thread class is located in java Thread package

  • C. 

    The run() method is used to start the thread

  • D. 

    The working mode of CPU using time slice rotation allows multiple programs to occupy the CPU in turn to achieve the effect of running at the same time

6. (short answer) programming exercise: apply object serialization and object deserialization to write objects to the file, read the objects and input them to the console. design sketch:

Task requirements:

1. Create the Product class and declare its attributes: id, name,categories,price

2. Implement Serializable interface;

3. Define the Product class construction method;

4. In the Test class, create objects of the Product class: iphone,ipad,macbook,iwatch

5. Instantiate object input stream and object output stream;

6. Write four objects of the Product class;

7. Read four objects of four product classes from the file.

Product class:

import java.io.Serializable;
public class Product implements Serializable {
	private int ID;
	private String name;
	private String categories;
	private Double price;
	public Product() {
		
	}
	public Product(int iD, String name, String categories, Double price) {
		super();
		ID = iD;
		this.name = name;
		this.categories = categories;
		this.price = price;
	}
 
	public int getID() {
		return ID;
	}
 
	public void setID(int iD) {
		ID = iD;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public String getCategories() {
		return categories;
	}
 
	public void setCategories(String categories) {
		this.categories = categories;
	}
 
	public Double getPrice() {
		return price;
	}
 
	public void setPrice(Double price) {
		this.price = price;
	}
 
	public String toString() {
		return "product ID:" + ID + "\n Product name:" + name + "\n Product attributes:" + categories + "\n product price:" + price + "element";
	}
 
}

ProductTest class:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
public class ProductTest {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Product iphone = new Product(123, "iphone", "telephone", 4888.0);
		Product ipad = new Product(234, "ipd", "computer", 5088.0);
		Product macbook = new Product(345, "macbook", "computer", 10688.0);
		Product iwatch = new Product(256, "iwatch", "watch", 4799.0);
		try {
			FileOutputStream fos = new FileOutputStream("product.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			FileInputStream fis = new FileInputStream("product.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			oos.writeObject(iphone);
			oos.writeObject(ipad);
			oos.writeObject(macbook);
			oos.writeObject(iwatch);
			oos.flush();
			System.out.println("apple Series product information:");
			
			// Method 1 output product information
			Product pro = (Product) ois.readObject();
			Product pro1= (Product) ois.readObject();
			Product pro2 = (Product) ois.readObject();
			Product pro3 = (Product) ois.readObject();
			System.out.println("apple Series product information");
			System.out.println(pro);
			System.out.println();
			System.out.println(pro1);
			System.out.println();
			System.out.println(pro2);
			System.out.println();
			System.out.println(pro3);
			System.out.println();
			oos.close();
			fos.close();
			ois.close();
		} catch (FileNotFoundException e) {
//			 TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
 
	}
 
}

 

 

7. (single choice question) please read the following procedure, select the correct sentence sequence and fill in the horizontal line (select one)

Make the running result:

I.      oos.writeObject(doberman);

II.     Dog dog1=(Dog)ois.readObject();

III.    oos.write(doberman);

IV.    ObjectOutputStream oos=new ObjectOutputStream(fos);

V.     ObjectInputStream ois=new ObjectInputStream(new FileInputStream("Dog.txt"));

VI.    OutputStreamWriter oos=new OutputStreamWriter(fos);

VII.    String toString()

VIII.   String()

IX.     public class Dog implements Serializable

X.     public class Dog extends Serializable

Correct answer: d my answer: D

  • A. 

    X VII  IV  V   I   II

  • B. 

    IX VIII  VI  V  III   II

  • C. 

    X  VIII  VI  V  III   II

  • D. 

    IX  VII  IV   V  I   II

8. (short answer) programming exercise: using system In input multiple lines of string from the keyboard, input one line and output one line from the console. At the same time, use PrintWriter to output to one line of the file until the input is "exit", and exit the program. Operation effect:

import java.io.*;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Test {
    public static void main(String[] args) throws IOException{
    	BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
		  BufferedWriter buf2 = new BufferedWriter(new FileWriter("test.txt"));
		  System.out.println("Please enter a lowercase English string");
		  String str = buf.readLine();
		   while(!str.equals("exit")){
			   System.out.println(str);
//			   System.out.println("program end");
		   buf2.write(str);
		   buf2.newLine();
		   System.out.println();
		   str = buf.readLine();
		    }
		   System.out.println("Program end");
		  buf.close();
		  buf2.close();
    }
}

 

 

 

Tags: Java

Posted by AnAmericanGunner on Thu, 02 Jun 2022 23:26:34 +0530