Basic Programming Exercises for Java Beginners

  1. Use the while loop and the if statement to calculate and output the even sum of 1-100. The loop variable is named i, and the variable that stores the sum is named sum.
    public class Test {
    	public static void main(String[] args) {
    		int sum=0,i=1;
    		while(i<=100)
    		{
    			if(i%2==0)
    			{
    				sum=sum+i;
    			}
    			i++;
    		}
    		System.out.println(sum);
    	}
    }

  2. Use the while loop and the if statement to calculate and output the sum of numbers divisible by 3 from 1 to 100. The loop variable is named i, and the variable that stores the sum is named sum.
    public class Test {
    	public static void main(String[] args) {
    		int sum=0,i=1;
    		while(i<=100)
    		{
    			if(i%3==0)
    			{
    				sum=sum+i;
    			}
    			i++;
    		}
    		System.out.println(sum);
    	}
    }

  3. Use the for loop and the if statement to output the numbers from 1 to 100 that are divisible by 7 or the single digit is 7, and the loop variable name is i.
    public class Test {
    		public static void main(String[] args) {
    		int sum=0,i=1;
    		while(i<=100)
    		{
    			if(i%7==0 || i%10==7)
    			{
    				sum=sum+i;
    			}
    			i++;
    		}
    		System.out.println(sum);
    	}
    }

  4. Write an application program to calculate the factorial of an integer n, the initial value of n is 8, and output the result, the loop variable is named i, and the variable that stores the factorial is named p.
    public class test
    {	
    	public static void main(String[] args) {
    		int n=8,i=1,p=1;
    		i=n;
    		while(i>0)
    		{
    			p=p*i;
    			i--;
    		}
    		System.out.println(p);
    	}
    }

  5. Use the while loop and the if statement to calculate and output the odd sum of 1-100. The loop variable is named i, and the variable that stores the sum is named sum.
    public class Test {
    	public static void main(String[] args) {
    		int sum=0,i=1;
    		while(i<=100)
    		{
    			if(i%2==1)
    			{
    				sum=sum+i;
    			}
    			i++;
    		}
    		System.out.println(sum);
    	}
    }

  6. Suppose an employee's annual salary (salary) this year is 80,000 yuan, and the annual salary growth rate is 6%. Write a Java application to calculate the employee's annual salary 10 years later, and count the total income for the next 10 years (from this year). The loop variable is named i, and the variable for total income is named total.
    public class Prog1{
    	public static void main(String[] args) {
    		int i;
    		double salary=80000,total=80000;
    		for(i=1;i<10;i++)
    		{
    			salary=salary*106/100;
    			total=total+salary;
    		}
    		System.out.println("10 years later,salary:"+salary);
    		System.out.println("10 years total:"+total);		
    	}
    }

  7. Create Rectangle class and object. 1) Create a Rectangle class; 2) Attributes: two double member variables, width and height; 3) Construction method without parameters: the initial values ​​of width and height are 6 and 8 respectively; 4) Method: calculate and output the rectangle The perimeter method is called findPremeter (); 5) Write a test class, create a Rectangle object named r, and call the perimeter and area methods.
    public class Rectangle
    {
    	private double width,height;
    	public Rectangle()
    	{
    		this.width=6;
    		this.height=8;
    	}
    	public double findPremeter()
    	{
    		return 2*(this.width+this.height);
    	}
    }
    
    
    public class Test
    {	
    	public static void main(String[] args) {
    		Rectangle r = new Rectangle();
    		System.out.println(r.findPremeter());
    	}
    }

  8. Create a class Student, the attributes include the usual grade (pingshi), the final grade (qimo); the construction method without parameters, the method has the method calculateScore() to calculate and output the total score, and the calculation method is: total score = normal grade + final grade 1/2; Create a test class, create a Student object s, and then call the calculateScore() method to output the total score.
    public class Student
    {
    	public int pingshi,qimo;
    	public Student(){}
    	public int calculateScore()
    	{
    		return pingshi+qimo/2;
    	}
    }
    
    
    public class Test
    {	
    	public static void main(String[] args) {
    		Student s = new Student();
    		s.pingshi=40;
    		s.qimo=50;
    		System.out.println(s.calculateScore());
    	}
    }

  9. Define the Circle class, which contains a property named radius, the type is int, a construction method without parameters and a method to calculate and output the area findArea (area=3.14*radius*radius), write a test class, and create an object c of the Circle class, Call the findArea method.
    class Circle
    {
    	public int radius;
    	public Circle(){}
    	public double findArea()
    	{
    		return radius*radius*3.14;
    	}
    }
    
    
    public class Test
    {	
    	public static void main(String[] args) {
    		Circle c = new Circle();
    	c.radius=2;
    		System.out.println(c.findArea());
    	}
    }

  10. Define a Book class, which represents a book. The attributes of this class include name (book title), author (author name), price (book price), define a construction method without parameters, and define a show method for outputting basic book information. Write a test class, create an object b of the Book class, and call the show method.
    public class Book
    {
    	public String name,author;
    	public double price;
    	public Book(){}
    	public void show()	
    	{
    		System.out.println("name:"+this.name);
    		System.out.println("author:"+this.author);
    		System.out.println("price:"+this.price);
    	}
    }
    
    
    public class Test
    {	
    	public static void main(String[] args) {
    		Book b = new Book();
    b.name="Java Program";
    		b.author="panda";
    		b.price=3.14;
    		b.show();
    	}
    }

  11. Define a product class Goods, which has three attributes: product name (String), product number (String) and price (double). There is a construction method with or without parameters and a method for calculating discounted prices. The method head is public void computeDiscout ( double percent), where the formal parameter represents the percentage of the discount. Write a test class, create an object of the product class, and call the method of calculating the discount price.
    public class Goods
    {
    public String name,number;
    			public double price;
    public Goods(){}
    public void computeDiscout(double percent)	
    {
    	System.out.println(price*percent/100);
    }
    }
    
    
    public class Test
    {	
    public static void main(String[] args) {
    	Goods g = new Goods();
    	g.name="Java Book";
    	g.number="G001";
    	g.price=3.14;
    	g.computeDiscout(90);
    }
    }
    

  12. The existing parent class Person has the following structure:
    class Person {
    	String id;
    	String name;
    
    	Person(String id, String name) {
    		this.id = id;
    		this.name = name;
    	}
    
    	void print() {
    		System.out.println("id =" + id + ",name =" + name);
    	}
    }
    在此基础上派生出子类Teacher,子类定义了自己的属性教师编号(teacherID),
    有不带参数的构造方法,覆盖了父类的print方法,调用父类被覆盖的print方法,
    增加打印自己的属性的语句,请实现Teacher类的编写。
    class Teacher extends Person
    {
    private String teacherID;
    Teacher(){
    	super(null,null);
    }
    public void print()
    {
    	super.print();
    	System.out.println("teacherID:"+this.teacherID);
    }
    }

  13. The existing parent class Good has the following structure:
    class Goods {
    	double unitPrice;//单价
    	double account;//数量
    	Goods(double unitPrice, double account) {
    		this.unitPrice=unitPrice ;
    		this.account=account ;
    	}
    	double totalPrice() {//计算总价格
    		return unitPrice * account;
    	}
    	void show() {
    		System.out.println("单价是" + unitPrice);
    		System.out.println("购买数量为" + account);
    		System.out.println("购买商品的总金额是" + this.totalPrice());
    	}
    }
    在此基础上派生出子类Milk,子类定义了自己的属性会员价格(vipPrice),
    有不带参数的构造方法,覆盖了父类的show方法,调用父类被覆盖的show方法,
    增加打印自己的属性的语句,请实现Milk类的编写。
    class Milk extends Goods
    {
    	private double vipPrice;
    	Milk()
    	{
    		super(0,0);
    	}
    	void show()
    	{
    		super.show();
    		System.out.println(this.vipPrice);
    	}
    }

  14. Write a program to simulate the "Challenge Cup" speech contest. A total of 10 judges will score. The score is a random number between 1 and 10. Store the 10 scores in an array of int type, and use the for loop to calculate the final score of the singer.
    public class Test {
       public static void main(String[] args) {
    	 	int[] score = new int[10];
    		int max=1,min=10,avg=0,sum=0;
    		for(int i=0;i<10;i++)
    		{
    			score[i]=(int)(1+Math.random()*(10-1+1));
    			//System.out.println(score[i]);
    		}
    		for(int i=0;i<10;i++)
    		{
    			if(score[i]>max)
    				max=score[i];
    			if(score[i]<min)
    				min=score[i];
    			sum=sum+score[i];
    		}
    		avg=sum/10;
    
    	 System.out.println("max= "+max);//最高分
    	 System.out.println("min= "+min);//最低分
    	 System.out.println("avg= "+avg);//平均分
    	}
    }

  15. An array a of int type is known, and the elements of the array are {12, 11, 78, 34} respectively. Write a program to use the for loop to output the array in reverse order.
    public class Test {
    	public static void main(String[] args) {
    		int[] a = {12,11,78,34};
    		int i=a.length;
    		for(i=i-1;i>=0;i--)
    			System.out.print(a[i]);
    	}
    }

  16. Define a Boolean array with a length of 100, the array name is fig, and use the for loop statement to assign false to all elements of the array.
    public class Test {
    	public static void main(String[] args) {
    		int[] a = {34,56,78,89,90,100};
    		for(int i=0;i<a.length;i++)
    			System.out.print(a[i]/10%10);
    
       }
    }

  17. A certain singer participates in the Song Grand Prix, and 20 judges will score her, which will be stored in an array score[], and the score will be a random number between 1 and 100. The program uses a for loop to output the highest score of the contestant , minimum score and final score (final score = total score / total number of judges)
    public class Test {
       public static void main(String[] args) {
    	 	int[] score = new int[20];
    		int max=1,min=100,avg=0,sum=0;
    		for(int i=0;i<20;i++)
    		{
    			score[i]=(int)(1+Math.random()*(100-1+1));
    			//System.out.println(score[i]);
    		}
    		for(int i=0;i<20;i++)
    		{
    			if(score[i]>max)
    				max=score[i];
    			if(score[i]<min)
    				min=score[i];
    			sum=sum+score[i];
    		}
    		avg=sum/20;
    
    	 System.out.println("max= "+max);//最高分
    	 System.out.println("min= "+min);//最低分
    	 System.out.println("avg= "+avg);//平均分
    	}
    }

  18. The following are the Name class, Person class, and Test class. Please fill in the accessor methods of all private data fields in the Name class and Person class.

    class Name
    {
    	private String firstName;//姓
    	private String lastName;//名
    	Name(String f,String l)
    	{
    		firstName=f;
    		lastName=l;
    	}
    	//填写访问器方法
    	public String toString()
    	{
    		return firstName + lastName;
    	}
    }
    class Person
    {
    	private Name name;//姓名
    	Person(Name n)
    	{
    		name=n;
    	}
    //填写访问器方法
    }
    class Test
    {
    	public static void main(String[] args)
    	{
    		Name theName=new Name("张","三");
    		Person p=new Person(theName);
    		System.out.println(p.getName());//输出结果:张三
    	}
    }
    class Name
    {
    	private String firstName;//姓
    	private String lastName;//名
    	Name(String f,String l)
    	{
    		firstName=f;
    		lastName=l;
    	}
    	public String getFirstName()
    	{
    		return firstName;
    	}
    	public void setFirstName(String fn)
    	{
    		firstName=fn;
    	}
    	public String getLastName()
    	{
    		return lastName;
    	}
    	public void setLastName(String ln)
    	{
    		lastName=ln;
    	}
    	public String toString()
    	{
    		return firstName + lastName;
    	}
    	
    }
    class Person
    {
    	private Name name;//姓名
    	Person(Name n)
    	{
    		name=n;
    	}
    	public Name getName()
    	{
    		return name;
    	}  
    	public void setName(Name n)
    	{
    		name=n;
    	}
    }
    class Test
    {
    	public static void main(String[] args)
    	{
    		Name theName=new Name("张","三");
    		Person p=new Person(theName);
    		System.out.println(p.getName());//输出结果:张三
    	}
    }

 

Guess you like

Origin blog.csdn.net/shanhe_yuchuan/article/details/122311891