第二周 - Elementary Programming

2.9 Are the following statements correct? If so,show the output.

public class Demo {
	public static void main(String[] args) {
		System.out.println("25/4 is " + 25 / 4);
		System.out.println("25/4.0 is " + 25 / 4.0);
		System.out.println("3*2/4 is " + 3 * 2 / 4);
		System.out.println("3.0*2/4 is " + 3.0 * 2 / 4);
	}
}



2.18 Show the following output.

public class Demo {
public static void main(String[] args) {
float f = 12.5F;
int i = (int) f;
System.out.println("f is " + f);
System.out.println("i is " + i);

}


2.20 Which of the following are correct literals for characters?

public class Demo {
	public static void main(String[] args) {
		float f = 12.5F;
		int i = (int) f;
		System.out.println('1');
		System.out.println('\u3fFa');
		System.out.println('\b');

	}
}
 The '\u345dE' and \t is wrong.



2.21 How do you display characters \ and "?

public class Demo {
	public static void main(String[] args) {
		System.out.println("\\");
		System.out.println("\"");
	}
}



2.22 Evaluate the following:

public class Demo {
	public static void main(String[] args) {
		int i = '1';
		int j = '1' + '2';
		int k = 'a';
		char c = 90;
		System.out.println("i=" + i + " j=" + j + " k=" + k + " c=" + c);

	}
}


2.23 Can the following conversions involving casting be allowed? If so, find the converted result.

public class Demo {
	public static void main(String[] args) {
		char c = 'A';
		float f = 1000.34f;
		int i = (int) f;
		i = (int) c;
		System.out.println("i=" + i + " c=" + c + " f=" + f);
	}
}

public class Demo {
	public static void main(String[] args) {
		double d = 1000.34;
		int i = (int) d;
		System.out.println("i=" + i + " d=" + d);
	}
}

public class Demo {
	public static void main(String[] args) {
		int i = 97;
		char c = (char) i;
		System.out.println("i=" + i + " c=" + c);
	}
}

2.24 Show the output of the following program:

public class Demo {
	public static void main(String[] args) {
		char x = 'a';
		char y = 'c';

		System.out.println(++x);
		System.out.println(y++);
		System.out.println(x - y);
	}
}


2.25 Show the output of the following statements:

public class Demo {
	public static void main(String[] args) {
		System.out.println("1" + 1);
		System.out.println('1' + 1);
		System.out.println("1" + 1 + 1);
		System.out.println("1" + (1 + 1));
		System.out.println('1' + 1 + 1);
	}
}


2.26 Evaluate the following expressions:

public class Demo {
	public static void main(String[] args) {
		System.out.println(1 + "welcome" + 1 + 1);
		System.out.println(1 + "welcome" + (1 + 1));
		System.out.println(1 + "welcome" + ('\u0001' + 1));
		System.out.println(1 + "welcome" + 'a' + 1);
	}
}

2.6** Write a program that reads an integer between 0 and 100 and adds all the digits in the integer.For example,if an integer is 932,the sum of all its digits is 14.

import javax.swing.JOptionPane;
public class Demo {
	public static void main(String[] args) {
		int sum=0,number;
		String input=JOptionPane.showInputDialog("Please input a number:");
		number=Integer.parseInt(input);
		while(number>0){
			sum=sum+(number%10);
			number/=10;
		}
		JOptionPane.showMessageDialog(null, "The digits is "+sum);
	}
}

import java.util.Scanner;
public class Demo {
	public static void main(String[] args) {
		int sum=0,number;
		//String input=JOptionPane.showInputDialog("Please input a number:");
		Scanner input=new Scanner(System.in);
		number=input.nextInt();
		while(number>0){
			sum=sum+(number%10);
			number/=10;
		}
		//JOptionPane.showMessageDialog(null, "The digits is "+sum);
		System.out.println("The digits is "+sum);
	}
}


2.24 Give an airplane's acceleration a and take-off speed v,you can compute the minimum runway length needed for an airplane to take off using the following formula:

length=v*v/2a

write a program that prompts the user to enter v in m/s and the acceleration a in m/s*s,and display the minimum runway length.

import javax.swing.JOptionPane;

public class Demo {
	public static void main(String[] args) {
		String v, a;
		double v0, a0, length;
		v = JOptionPane.showInputDialog("Please input the speed v:");
		a = JOptionPane.showInputDialog("Please input the acceleration a:");
		v0 = Double.parseDouble(v);
		a0 = Double.parseDouble(a);
		length = (v0 * v0) / (2 * a0);
		java.text.DecimalFormat df = new java.text.DecimalFormat("#.###");
		JOptionPane.showMessageDialog(null,
				"The minimum length is " + df.format(length) + "m.", "Result",
				JOptionPane.INFORMATION_MESSAGE);
	}
}

import java.util.Scanner;

public class welcome {
	public static void main(String[] args) {
		double v0, a0, length;
		Scanner input = new Scanner(System.in);
		v0 = input.nextDouble();
		a0 = input.nextDouble();
		length = (v0 * v0) / (2 * a0);
		java.text.DecimalFormat df = new java.text.DecimalFormat("#.###");
		System.out.println("The minimum length is " + df.format(length) + "m.");
	}
}









 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值