楼主: ReneeBK
2307 11

K. Rajkumar: JAVA Programming, 1/e [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4897份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49635 个
通用积分
55.7537
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2015-5-31 03:34:32 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

  • JAVA Programming, 1/e
  • By: K. Rajkumar

  • Publisher: Pearson India

  • Pub. Date: May 1, 2013

  • Web ISBN-13: 978-9-3-3251787-5

  • Pages in Print Edition: 696

  • Subscriber Rating: [0 Ratings]



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Program Kumar gram Ming JAVA

本帖被以下文库推荐

沙发
ReneeBK 发表于 2015-5-31 03:35:56
Listing 4.5. Nested Function Calls

  1. //DupGenerator.java
  2. public class DupGenerator
  3. {
  4.   public static String duplicate(String s)
  5.   {
  6.     return s + s;
  7.   }

  8.   public static void main(String[] args)
  9.   {
  10.     String s = duplicate(“Java”);
  11.     System.out.println(s); // prints Java two times

  12.     s = duplicate(duplicate(duplicate(“Hello”)));
  13.     System.out.println(s); // 8 times
  14.   }
  15. }
复制代码

藤椅
ReneeBK 发表于 2015-5-31 03:37:07
Listing 4.6. Calculating the Area of Geometric Objects
  1. // AreaFunction.java

  2. import java.io.*;

  3. public class AreaFunction
  4. {
  5.   // receives side and returns area public static int areaSquare(int a)
  6.   {
  7.     return a * a;
  8.   }

  9.   // receives sides a and b, returns area
  10.   public static int areaRectangle(int a, int b)
  11.   {
  12.     return a * b;
  13.   }

  14.   // receives sides a and b, returns area
  15.   public static double areaTriangle(int a, int b)
  16.   {
  17.     return 0.5 * a * b;
  18.   }

  19.   // receives radius and returns area
  20.   public static double areaCircle(int r)
  21.   {
  22.     return 2 * 3.1415 * r;
  23.   }

  24.   // receives radius and height, returns area
  25.   public static double areaCylinder(int r, int h)
  26.   {
  27.     return 2 * 3.1415 * r * r + 2 * 3.1415 * r * h;
  28.   }

  29.   public static void main(String[] args)
  30.   {
  31.     int area = areaSquare(5);
  32.     System.out.println(area);

  33.     // alternatively println() method can also call a method directly
  34.     System.out.println(areaSquare(5));
  35.     System.out.println(areaRectangle(5,3));
  36.     System.out.println(areaTriangle(5,3));
  37.     System.out.println(areaCircle(3));
  38.     System.out.println(areaCylinder(3,4));
  39.   }
  40. }
复制代码

板凳
ReneeBK 发表于 2015-5-31 03:39:10
Listing 4.7. Generate all Prime Numbers from 2 to 500
  1. // PrimeNumber.java

  2. public class PrimeNumber
  3. {
  4.   public static boolean isPrime(int n)
  5.   {
  6.     boolean prime = true;
  7.     int max = (int)Math.sqrt(n);

  8.     for (int i = 2; i <= max; i++)
  9.     {
  10.       if (n % i == 0)
  11.       {
  12.         prime = false;
  13.         break;
  14.       }
  15.     }

  16.     return prime;
  17.   }

  18.   public static void main(String[] args)
  19.   {
  20.     for(int i = 2; i <= 500; i++)
  21.     {
  22.       if(isPrime(i))
  23.         System.out.print(i + “ ”);
  24.     }
  25.   }
  26. }
复制代码

报纸
ReneeBK 发表于 2015-5-31 03:40:28
Listing 4.8. Factorial Using Loops
  1. // Factorial.java
  2. import java.util.*;

  3. public class Factorial
  4. {
  5.   public static int fact(int n)
  6.   {
  7.     int f = 1;

  8.     if( n == 1)
  9.       return f;
  10.     for(int i = 2; i <= n; i++)
  11.       f = f * i;

  12.     return f;
  13.   }
  14.   public static void main(String[] args)
  15.   {
  16.     Scanner sc = new Scanner(System.in);
  17.     System.out.print(“Enter a number: ”);
  18.     int n = sc.nextInt();

  19.     if (n >= 0)
  20.       System.out.println(“Factorial of ”+ n + “: ” + fact(n));
  21.   }
  22. }
复制代码

地板
ReneeBK 发表于 2015-5-31 03:42:08
Listing 4.12. Addition Calculator Using Overloading
  1. // AdditionCalculator.java

  2. public class AdditionCalculator
  3. {
  4.   public static int add(int i, int j)
  5.   {
  6.     return i + j;
  7.   }

  8.   public static double add(double i, double j)
  9.   {
  10.     return i + j;
  11.   }

  12.   public static float add(float i, float j)
  13.   {
  14.     return i + j;
  15.   }

  16.   public static double add(int i, double j)
  17.   {
  18.     return i + j;
  19.   }

  20.   public static double add(double i, int j)
  21.   {
  22.     return i + j;
  23.   }

  24.   public static void main(String[] args)
  25.   {
  26.     System.out.println(add(10,20));
  27.     System.out.println(add(10.0,20.0));
  28.     System.out.println(add(100.0f,200.0f));
  29.     System.out.println(add(30,20.0));
  30.     System.out.println(add(50.0,20));
  31.   }
  32. }
复制代码

7
ReneeBK 发表于 2015-5-31 03:43:16
Listing 4.13. Sorting Numbers Application
  1. // SortNumbers.java

  2. import java.io.*;

  3. public class SortNumbers
  4. {
  5.   public static void sort(int[] numbers) // reference numbers will directly modify original array a in main()
  6.   {
  7.     // take ith number
  8.     for (int i = 0; i < numbers.length-1; i++)
  9.     {
  10.       // compare ith number with all other jth numbers
  11.       for (int j = j < numbers.length;
  12.       {
  13.         // if second number < first
  14.         if(numbers[j] < numbers[i])
  15.         {
  16.           // interchange
  17.           int temp;
  18.           temp = numbers[i];
  19.           numbers[i] = numbers[j];
  20.           numbers[j] = temp;
  21.         }
  22.       }
  23.     }
  24.     // since numbers is a reference to original array, through numbers original array gets sorted, so nothing has to be returned
  25.   }

  26.   public static void main(String[ ] args) throws IOException
  27.   {
  28.     BufferedReader in = new BufferedReade (new InputStreamReader(System.in));

  29.     // read n
  30.     System.out.print(“Enter total no. to be processed: ”);
  31.     int n = Integer.parseInt(in.readLine());

  32.     // create array a of n size
  33.     int[ ] a = new int[n]; // to keep actual numbers

  34.     // read actual numbers
  35.     System.out.println(“Enter actual numbers: ”);
  36.     for (int i = 0; i < n; i++)
  37.       a[i] = Integer.parseInt(in.readLine());

  38.     // show
  39.     System.out.println(“Original numbers before sorting ”);
  40.     for (int i = 0; i < n; i++)
  41.       System.out.print( a[i] + “ ”);

  42.     // call sort() method with array a
  43.     sort(a);

  44.     // show sorted sequence
  45.     System.out.println(“\nSorted sequence of numbers ”);
  46.     for (int i = 0; i < n; i++)
  47.       System.out.print( a[i] + “ ”);
  48.   }
  49. }
复制代码

8
ReneeBK 发表于 2015-5-31 03:45:04
Listing 4.14. Sort() to Sort Strings
  1. Listing 4.14. Sort() to Sort Strings

  2. public static void sort(String[ ] names)
  3. {
  4.   // take ith name
  5.   for (int i = 0; i < names.length-1; i++)
  6.   {
  7.     // compare ith name with all other jth names
  8.     for (int j = j < names.length; j++)
  9.     {
  10.       // if second number < first
  11.       if(names[j].compareTo(names[i]) < 0)
  12.       {
  13.         // interchange values
  14.         String temp;
  15.         temp = names[i];
  16.         names[i] = names[j];
  17.         names[j] = temp;
  18.       }
  19.     }
  20.   }
  21. }
复制代码

9
ReneeBK 发表于 2015-5-31 03:46:22
Listing 4.15. Sorting Names
  1. Listing 4.15. Sorting Names

  2. // SortNames.java
  3. import java.io.*;

  4. public class SortNames
  5. {
  6.   public static void sort(String[] names)
  7.   {
  8.     // take ith name
  9.     for (int i = 0; i < names.length-1; i++)
  10.     {
  11.       // compare ith name with all other jth names
  12.       for (int j = i+1; j < names.length; j++)
  13.       {
  14.         // if second number < first
  15.         if(names[j].compareTo(names[i]) < 0)
  16.         {
  17.           // interchange values
  18.           String temp;
  19.           temp = names[i];
  20.           names[i] = names[j];
  21.           names[j] = temp;
  22.         }
  23.       }
  24.     }
  25.   }

  26.   public static void main(String[] args) throws IOException
  27.   {
  28.     BufferedReader in = new BufferedReader (new InputStreamReader(System.in));

  29.     // read numbers
  30.     System.out.print(“Enter total no. of names to be processed: ”);
  31.     int n = Integer.parseInt(in.readLine());
  32.     String a[] = new String[n]; // to keep actual names

  33.     System.out.println(“Enter actual names: ”);
  34.     for (int i = 0; i < n; i++)
  35.       [i] = in.readLine();

  36.     // sort names
  37.     sort(a);

  38.     // show
  39.     System.out.print(“Sorted names: ”);
  40.     for (int i = 0; i < n; i++)
  41.       System.out.print( a[i] + “ ”);
  42.   }
  43. }
复制代码

10
ReneeBK 发表于 2015-5-31 03:48:03
Listing 4.16. Recipes Application

  1. Listing 4.16. Recipes Application

  2. // Recipes.java

  3. public class Recipes
  4. {
  5.   public static int calcIngredient( int[] serv, int[][] flour, int column )
  6.   {
  7.     int total = 0, i, j;

  8.     for( i = 0, j = 0; i < serv.length; i++, j++)
  9.       total = total + serv[i] * flour[j][column]; // only one column in flour required

  10.     return total;
  11.   }

  12.   public static void main(String[] args)
  13.   {
  14.     int[ ][ ] recipe = { {2,2,1,0}, {3,0,1,0}, {2,1,2,1}};
  15.     int[ ] serving = {1, 4, 3};

  16.     // call to calculate flour
  17.     int cups = calcIngredient(serving, recipe, 0); // only oth column required
  18.     System.out.println(“No. of cups of flour required: ” + cups);

  19.     // call to calculate eggs
  20.     int eggs = calcIngredient(serving, recipe, 1); // only 1st column required
  21.     System.out.println(“No. of eggs required: ” + eggs);

  22.     // call to calculate milk
  23.     int milk = calcIngredient(serving, recipe, 2); // only 2nd column required
  24.     System.out.println(“No. of cups of milk required: ” + milk);

  25.     // call to calculate oil
  26.     int oil = calcIngredient(serving, recipe, 3); // only 3rd column required
  27.     System.out.println(“No. of tablespoons of oil required: ” + oil);
  28.   }
  29. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-2 18:26