forked from okaram/IntroJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.java
More file actions
134 lines (109 loc) · 3.1 KB
/
test.java
File metadata and controls
134 lines (109 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
int a=10, b;
a=a+5; // a=15
b=a+4; // b=19
b= (++a); // a=16, b=16
int ans=a+b; //16+19=35
//Write a function called isOdd that takes an int and returns a boolean;
//the function returns true if the number is odd and false otherwise
public static
boolean isOdd(int a) {
return (a%2==1);
}
//Write a function called isLarge that takes an int and returns a boolean;
// the function returns true if its argument is larger than 1000, false otherwise.
boolean isLarge(int n){
return n>1000;
}
//Write a function called fizz, that takes an integer and returns a String.
//The function returns “fizz” if its argument is divisible by 3, and the
//number otherwise
public static
String fizz(int a) {
return a%3==0 ? "fizz" : ""+a;
/* if(a%3==0)
return "fizz";
else
return ""+a;
*/
}
public static
int bar(int a) {
if (a<=0)
return 2;
else
return a*bar(a-1);
}
bar(-1) ? = 2
bar(3) = 3*bar(2) = 3* 2 *bar(1) = 3 * 2 * 2 = 18
public static
String baz(char c1, char c2) {
String ans="";
for(char c=c1; c<=c2; ++c)
ans+=c;
return ans;
}
baz('a','d') ?
//Write a function called printFromTo, which takes two integer parameters,
// and prints all numbers between its first and second parameters,
// including both parameters (print a newLine character after each number).
void printFromTo(int from, int to) {
for(int i=from; i<=to; ++i) {
System.out.println(i);
}
}
void printFromTo(int from, int to) {
int i=from;
while( i<=to) {
System.out.println(i);
++i;
}
}
void printFromTo(int from, int to) {
if(from<=to) {
System.out.println(from);
printFromTo(from+1,to);
}
}
void printFromTo2(int from, int to) {
for(int i=from+1; i<to; ++i) {
System.out.println(i);
}
}
void printDownFromTo(int from, int to) {
for(int i=from; i>=to; --i) {
System.out.println(i);
}
}
Using a while loop (and no recursion or other kinds of loops),
write a function called power, that takes two integer parameters,
say base and exponent, and returns the first parameter (base)
raised to the second parameter (exponent), by performing repeated
multiplications.
a. now do it with a for loop
b. and now do it using recursion
int power (int base, int exp){
int value=1;
while(exp>0){
value=value*base;
--exp;
}
return value;
}
int power (int base, int exp){
int value=1;
for( ; exp>0 ; value*=base, --exp)
;
return value;
}
int power(int base, int exp) {
return base==0 ? 1 : base*power(base,exp-1);
}
public static f2(String s, char c)
{
for(int i=0; i<s.length();++i)
if(s.charAt(i)==c)
return true;
return false;
}
f2(“Hello”,’a’) ? false
f2(“Hello”,’l’) ? true