Integer to char - cast and print: [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to know the logic behind this programm and (char) cast.
How does it work and how it is printing all the letters, symbols and numbers
package ascii1
public class Ascii1 {
public static void main(String[] args) {
int i=1;
while(i<122)
{
System.out.println((char)i+"\t");
if (i%10==0)
System.out.println("");
i++;
}
}
}
Its output is:
//Blanks in the beginning...
! "
$ % & ' (
)
*
+ ,
- . / 0 1 2
3 4 5 6 7 8 9 : ; <
=
? # A B C D E F
G H I J K L M N O P
Q R S T U V W X Y Z
[ \ ] ^
_ ` a b c d
e f g h i j k l m n
o p q r s t u v w x
y BUILD SUCCESSFUL (total time: 0 seconds)

Using ASCII representation, every char has a numerical value.
When you iterate, adding +1 to the i variable, you get to numbers on the ASCII table representing some characters.
Finally, the (char) cast returns the above ASCII character.

Related

Java conversion table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 days ago.
This post was edited and submitted for review 8 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm writing a program to print out a unit conversion table for lengths.
But I'm getting stuck on formatting the answers in a table array like this:
feet miles kilometers | kilometers miles feet
5280 1 1.609 | 1 0.622 3281.541
10560 2 2.218 | 2 1.243 6563.083
21120 4 6.436 | 4 2.486 12126.165
[![expected result][1]][1]
Here is the code I've written so far:
class Main {
public static void main(String[] args) {
// values below:
//int m;
int mil;
//double fe;
double ki;
double mfe = 5280;
double kilo = 1.609;
double kf = 0.000189394;
System.out.println("feet: miles: kilometers: | kilometers: miles: feet:");
for (int m = 1, fe = 1; m <= 15 && fe <= 15; m++){
System.out.println(m);
System.out.printf("%.3f", (m * mfe));
System.out.printf("%.3f", (m * kilo));
System.out.println(" | ");
}
for (int k = 1, m = 1; k <= 15 && m <= 15; k++){
System.out.printf("%-11d%-10.3f", k, (k / kilo));
System.out.printf("%.3f", (k * kf));
}
}
}
When I run this code, it produces:
feet: miles: kilometers: | kilometers: miles: feet:
1
5280.0001.609 |
2
10560.0003.218 |
3
15840.0004.827 |
4
...
73920.00022.526 |
15
79200.00024.135 |
1 0.622 0.0002 1.243 0.0003 1.865 0.0014
I'm trying to get the code to print to 3 decimal places in a table array like stated above.
Towards the two goals I mentioned above, I'm having the following difficulties:
How can I get the two lists to print side by side?
How can I get the right amount of whitespace between the numbers?

Read n, Find the value of M where M = 2 * 4 * 6 * … * ≤ n [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
n is any integer value, given by the user. M is Multiply of all even numbers in 1..n:
M = 2 * 4 * 6 * … * ≤ n
Example :
int n = 9
output
int output = 2*4*6*8; // 384
My code:
Scanner in = new Scanner(System.in)
int n=inut.nextInt();
for(....)
In short you need to multiply all even numbers between 1 and n.
For this you can use a for-loop and if-statement. for-loop will give you all numbers between 1..n, and if-statement will reject odd numbers, leaving only even.
Last path would be to multiply all values.
int n = 9;// input;
int result = 1; // because you are multiplying, initial result must be 1
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) { // ignore all odd numbers
result *= i; // multiply result with next even value
}
}
System.out.println(result); // print the result: 384
You can look at this like an assembly line. At the start someone is generating numbers from 1 to n. Then someone called 'the filter' rejects (push to trash) all odd numbers, at the end of the line someone called 'the aggregator' multiplies all values into an result.
With Java 8 and streams this can be represented by:
int result = IntStream.range(1,n)// generate numbers from 1 to n
.filter(value->value%2==0) // reject all odd numbers
.reduce(1, (a,b)-> a*b); // multiple all values, with 1 as initial result
System.out.println(result);

take input until input positive number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Take two integers p & q ... Sum should be printed of p to next q numbers including p .. if q <= 0 , then it will take the value of q again ...
Input :::: 3 2
Output :::: 7 (p=3 & q = 2 ..... So from 3 to next 2 numbers are 3 & 4 as it will be included in the sum ... Now we will have to print the sum of 3+4 and that's 7 )
Input ::: 4 -1 1
Output :::. 4 ( as the next number is 4 )
That means we have to start counting from the taken integer ....
Solve it and drop the solution here ......
import java.util.Scanner;
public class Ahmed {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int p=sc.nextInt();
int q=sc.nextInt();
int sum=0;
if(q>=0){
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
else{
q=sc.nextInt();
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}
System.out.println(sum);
}
}
if I take input 4 -1 -1 there is a error. Loop will continue until I take q input a positive number or 0;
Correct input 4 -1 -1 2 output 9.
Generally, when we have a loop and we dont know how many times it will repeat, we can use while.
Create a function like this:
private int readPositiveInt(Scanner sc){
int i = -1
while (i <= 0) {
i = sc.nextInt();
}
return i;
}
Then, you replace this line of code int q=sc.nextInt(); by this:
int q = readPositiveInt(sc);
Finally, once q will be positive for sure, you can remove these lines:
else{
q=sc.nextInt();
for(int i=1; i<=q ;i++) {
sum=p+sum;
p++;
}
}

How to write a program that returns number of occurrences of a string in another string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I'm new to programming. I'm using java. Right now I have an assignment I can't solve on a website that teaches java.
This is the assignment
Write a program that returns number of occurrences of a string in another string.
E.g
Input:
First String: the
Second String: The students are working hard in the faculty of Engineering because they love it
Output:
3
Note: You should only use nested loops. Don’t use methods like indexOf or substring.
Ienter image description here reached to the code the calculate the number of occurrences but it failed in case of repeated letters
E.g
input:
First String : ooo
Second String : wooooooooooooooooooooow
Output : 21
It's supposed to be 7 since the ooo have only repeated 7 times
This question can be simply solved by using z-function in linear time.
int NumberOfcopies(String t, String h){
// t = the first string i.e "ooo"
// h = the second string i.e "wooooooooooooooooooooow"
String s = t + "$" + h; // adding a sentinel character in between
int n = s.length(); // length of new string
int[] z = new int[n]; // z array
// Code ref : http://e-maxx-eng.github.io/string/z-function.html
int l = 0, r = 0;
for (int i = 1; i < n; i++){
if (i <= r)
z[i] = Math.min(r - i + 1, z[i - 1]);
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i]))
++z[i];
if (i + z[i] - 1 > r){
l = i;
r = i + z[i] - 1;
}
}
//checking for all the occurance of string t in string h
int res = 0;
for (int i = t.length() + 1; i < n; ){
if (z[i] == t.length()){
//A copy is found so skip next t.length chars
i += t.length();
++res;
}
else ++i;
}
System.out.println("Number of Occurance : " + res);
return res;
}
The Z-function for this string is an array of length n where
the i-th element is equal to the greatest number of characters
starting from the position i that coincide with the first
characters of s.
This can be exploited to find the number of occurrences of a string t in another string h. Suppose we join the string t and h with a sentinel character in between (Sentinel character is a character that does not occur in either of the strings) to form a new string s.
Lets us calculate the z function in array z[].
Now lets start searching from the character after the sentinel character i.e. the characters of string h. For i-th character in string h ( such that i-th character belongs to string s) if z[i] equals to length of string t (the pattern) then it implies that starting from i-th char, the t.length() chars are same as the first t.length() chars of string s which is what the string t equals.
example :
t = ab
h = aaabaab
s = a b $ a a a b a a b
z = 0 0 0 1 1 2 0 1 2 0
i = 0 1 2 3 4 5 6 7 8 9
for i = 5 we can see that z[i] == t.length(), that means we found a copy. Now to prevent Overlapping solutions, we skip the t.length() chars hence now i = 7
continuing this will get you the result.

Finding the sum of the two greatest numbers of 3 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With Java, how would one find the greatest two numbers of a set of 3 numbers without using if conditionals.
For example given the 3 numbers {2,3,5}
int a = 2;
int b = 3;
int c = 5;
int total;
total would be replaced with the value of c+b = 8
List<Integer> data = Arrays.asList(23,6,13);
Collections.sort(data);
Collections.reverse(data)
data = data.sublist(0,2);
System.out.println(data);
One line:
int biggest = (a > b ? a : b) > c ? (a > b ? a : b) : c;
Two lines:
int firstStep = (a > b ? a : b);
int biggest = firstStep > c ? firstStep : c;
Java 8:
int max = Arrays.stream(numbers).max().getAsInt();
int sec_max = Arrays.stream(numbers).filter(i -> i != max).max().getAsInt();

Categories