java codes
تعليم البرمجة
Example 1
write a program in Java that calculates the ratio of upper and lower case letters in the following text
"To be or not To be: That Is the Question"
so the code will be just as
public class test{
public static void main(String[]args){
String var_string = "To be or not To: That Is the Questtion";
double big_letters_per ,small_letters_per;
int big_letters_counter = 0,small_letters_counter = 0;
for(int index=0;index<var_string.length();index++){
if ((int)var_string.charAt(index)>=65 && (int)var_string.charAt(index)<=90) {
big_letters_counter = big_letters_counter+1;
}
if ((int)var_string.charAt(index)>=97 && (int)var_string.charAt(index)<=122) {
small_letters_counter = small_letters_counter+1;
}
}
big_letters_per = big_letters_counter*100/var_string.length();
small_letters_per = small_letters_counter*100/var_string.length();
System.out.println("The big letters percentage is "+big_letters_per+" %");
System.out.println("The small letters percentage is "+small_letters_per+" %");
System.out.println("The spaces percentage is "+(100-(small_letters_per+big_letters_per))+" %");
}
}
and the output will be as bellow
Example2
If A and B are two arrays with a certain number of integers, write a Java program to alternate the elements of the two arrays symmetrically without using other variables or matrices.
so the code will be just as
public class test{
public static void main(String[]args){
int [] A = {1,2,3,4,5};
int [] B = {6,7,8,9,10};
System.out.println("the items of array A before change are ");
for (int index=0; index<5; index++) {
System.out.print(A[index]+" , ");
}
System.out.println();
System.out.println();
System.out.println("the items of array B before change are ");
for (int index=0; index<5; index++) {
System.out.print(B[index]+" , ");
}
System.out.println();
System.out.println();
for (int index=0; index<5; index++) {
A[index] = A[index]+B[index];
B[index] = A[index]-B[index];
A[index] = A[index]-B[index];
}
System.out.println("_________________________________________________");
System.out.println("the items of array A after change are ");
for (int index=0; index<5; index++) {
System.out.print(A[index]+" , ");
}
System.out.println();
System.out.println("the items of array B after change are ");
for (int index=0; index<5; index++) {
System.out.print(B[index]+" , ");
}
System.out.println();
}
}
and the output will be as
Example 3
Q:
write a Java program to define the class "person".Contains the variable name_id, year of birth, and year, Then define the "student" class to inherit the elements of the first class, and add the variables "std_id", "dept", and the "prinrSTD" function to print the complete student data Then define the "test" class that contains the main function, and then execute the program with complete data for the student
So the code will be just as
public class test{
public static void main(String[]args){
student std = new student();
std.prinrSTD("Omer",1999,216001,"Mobile Computing");
}
}
public class student extends person{
int std_id;
String dept;
public static void prinrSTD(String p_name,int year,int std_id,String dept){
System.out.println("name : "+p_name);
System.out.println("Birth Year : "+year);
System.out.println("Student ID : "+std_id);
System.out.println("Department : "+dept);
}
}
public class person{
String p_name;
int year;
}
and the output will be as
*Note : you have to create each class in a different file in the third example