0% found this document useful (0 votes)
62 views3 pages

Java Code Examples and UML Overview

This document contains 4 code examples demonstrating different Java concepts: 1. An abstract Person class with subclasses Teacher and Student that demonstrate polymorphism. 2. A calculator program that takes user input for two numbers and an operation to perform basic math. 3. A program that sorts an integer array in ascending order. 4. A program that demonstrates various string methods like length, toLowerCase, replace, and substring.

Uploaded by

Tamer El Deeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

Java Code Examples and UML Overview

This document contains 4 code examples demonstrating different Java concepts: 1. An abstract Person class with subclasses Teacher and Student that demonstrate polymorphism. 2. A calculator program that takes user input for two numbers and an operation to perform basic math. 3. A program that sorts an integer array in ascending order. 4. A program that demonstrates various string methods like length, toLowerCase, replace, and substring.

Uploaded by

Tamer El Deeb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Java code using UML diagram


//abstract class
Public abstract class person{
Private String name;
Private int age;
Public void person(String n, int a){
[Link]= n;
[Link]= a;
}
Public abstract void print();
}
//teacher class
Public class teacher extends person{
Private float salary;
Public teacher(Sting n, int a, float s){
Super(name, age);
[Link]= s;
}
Public void print(){
[Link](“Teacher’s name is: ” + n);
[Link](“Teacher’s age is: ” + a);
[Link](“Teacher’s salary is: ” + s);
}
Public static void main(string args[]){
Person teacher = new teacher(n, a, s);
[Link]();
}
}
//student class
Public class student extends person{
Private int id;
Public teacher(Sting n, int a, int i){
Super(name, age);
[Link]= i;
}
Public void print(){
[Link](“student’s name is: ” + n);
[Link](“student’s age is: ” + a);
[Link](“student’s id is: ” + i);
}
Public static void main(string args[]){
Person student = new student(n, a, i);
[Link]();
}
}

2.
Import [Link]
Public class Main{
Public static void main(){
Int first_num , second_num;
Char operation;
//reading the first number
[Link](“Enter the first number, please!”);
first_num = (int)[Link]();
//reading the second number
[Link](“Enter the second number, please!”);
second_num = (int)[Link]();
//enter the operation to perform
[Link](“Enter the operation you want to perform, please!”);
operation = (int)[Link]();
//performing the required operation and viewing the answer
switch(operation){
case ‘+’:
[Link](“result=”+ first_num + second_num);
breake;
case ‘-’:
[Link](“result=”+ first_num - second_num);
breake;
case ‘*’:
[Link](“result=”+ first_num * second_num);
breake;
case ‘/’:
[Link](“result=”+ first_num / second_num);
breake;

default:
[Link](“invalid operation”);
break;
}
}
}
3.
Import [Link]
Public class Main{
Public static void main(){
//creating an array with 5 numbers
Int[] numbers= {5,10,17,2,25};
//printing the original array
[Link](“the original array : ”[Link](numbers));
//perform the sorting operation
[Link](numbers);
//printing the sorted array
[Link](“\n \the sorted array in assending order: ”[Link](numbers));
}
}
_____________________________________________________________________________________

4.
Import [Link]
Public class Test{
Public static void main(string args[]){
//creating the string
String str = new String(“KING KHALED UNIVERSITY”);
//finding the length of string
[Link](“the length of the string is: ” + [Link]());
//converting to lowercase
[Link](“the lowercase of the string is: ” + [Link]());
//replacing the character ‘K’ with the character ‘M’ in the word ‘KING’
[Link](“the original string before replace: ” + str);
String replaced = [Link]( “K” , “M” );
[Link](“the string after replace: ” + replaced);
//perform substring(3,9)
[Link]([Link](3,9));
}
}

You might also like