Java入门级题目及源代码(上)
来源:互联网 发布:华为工业级网络路由器 编辑:程序博客网 时间:2024/05/19 20:47
[Problem 1]
Use Java to implement the following algorithm:
Step 1: declare a variable named x and assign 20 to it.
Step 2: declare a variable name y and assign 5 to it.
Step 3 compute the product of x and y and store it in z.
Step 4: display the value of all 3 variables.
public class E1_1 {public static void main(String args[]){int x=20;int y=5;int z=x+y;System.out.println("x="+x);System.out.println("y="+y);System.out.println("z="+z);}}
[Problem 2]
1. Use Java to implement the following algorithm:
Step 1: declare a variable named value1 and assign 20.5 to it.
Step 2: declare a variable name value2 and assign 40.5 to it.
Step 3 compute the average of these 2 variables and store the result in another variable named ave.
Step 4: display the value of all 3 varaibles.
public class E1_2 {public static void main(String args[]){double value1=20.5;double value2=40.5;double ave=(value1+value2)/2;System.out.println("value1="+value1);System.out.println("value2="+value2);System.out.println("ave="+ave);}}
[Problem 3]
1. Write a Java program ConeVolume to compute and display the volume of a cone. All the necessary inputs should be obtained from the application runtime paramenters.
Volume of cone= 1/3*π*radiusOfBase2*height
Assume π=3.14
public class ConeVolume {public static void main(String args[]){double pi=3.14;double radiusOfBase=Double.parseDouble(args[0]);double height=Double.parseDouble(args[1]);double VolumeOfCone=(1.0/3)*pi*radiusOfBase*radiusOfBase*height;System.out.println("Volume of cone="+VolumeOfCone);}}
[Problem 4]
1. Write a Java program which generates 5 random numbers ranging from 16 to 30 (inclusive) and reports the highest number. Your program output should resemble the following (note: the numbers are random):
Number 1 : 15
Number 2 : 15
Number 3 : 25
Number 4 : 30
Number 5 : 18
Highest Number is 30
Hint: Math.random() generate a double number in 0-1.
Math.floor(): floor the double number
Or
import java.util.*
usage:
Random rand=new Random(50);
rand.nextInt(14): generate 0-14 random integer.
public class E1_4 {public static void main(String args[]){int max=0;int n[]=new int[5];for (int i=0;i<5;i++){n[i]=(int)(Math.floor(16+14*Math.random()));System.out.println("Number "+(i+1)+" : "+n[i]);if (n[i]>max)max=n[i];}System.out.println("Highest Number is "+max);}}
- Java入门级题目及源代码(上)
- Java入门级题目及源代码(下)
- 倒油题目(Java源代码)
- java经典笔试题目解析(上)
- 动态规划经典入门级题目*2及详解
- java入门之俄罗斯方块源代码
- [Activiti]源代码下载及解读入门
- poj3624及01背包入门题目
- 群论及Polya计数定理题目入门
- Java入门之一(上)----了解
- java字节码入门(上)
- 第一章 JAVA入门 Framework层 (上)
- (1)2017.8.3上-java入门
- sina网页新闻小偷原理及源代码(java版)
- sina网页新闻小偷原理及源代码(java版) .
- 2048小游戏(Java)源码解析及源代码打包
- java初中级工程师面试笔试题目及心得(全部实战经验)(答案)
- java初中级工程师面试笔试题目及心得(全部实战经验)(答案)
- Tomcat配置数据源(JNDI方式)
- 网站备份
- 解决Linux系统磁盘空间满的"软"办法
- 社会化推荐系统浅析-社会化推荐系统和传统搜索的区别及当前现状随谈
- TreeSet的使用。
- Java入门级题目及源代码(上)
- OpenGL反走样/抗锯齿实例
- android输入法框架整理
- Ubuntu10.04安装JDK1.6
- 18---java中泛型03(泛型的其他应用)
- SQL Server DBA工作内容详解
- sigaction函数 - 改变信号行为
- Android 中Message,MessageQueue,Looper,Handler详解+实例
- Android中ListView分页加载数据