Java Programming Exercises: Methods and Overloading, Summaries of Programming Languages

A series of java programming exercises focused on methods, method overloading, and related concepts. It includes code snippets, explanations, and challenges to help learners understand and apply these principles. Topics covered range from basic method declarations to more advanced concepts like method overloading and bonus challenges, making it a valuable resource for students learning java. The exercises are designed to reinforce understanding through practical application and problem-solving.

Typology: Summaries

2024/2025

Uploaded on 09/20/2025

alp-gencgil
alp-gencgil 🇭🇺

1 document

1 / 93

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Java
This is a Notion page that created by myself (alge) to teach myself. I thought I might help
the newbies.
For practice, visit codewars.com and set the exercise difficulty to 8 KYU. This will train you
on the concepts that the page teaches.
Java Fundamentals for Beginners: The guide provides a foundational introduction to
Java, starting with the interactive JShell.
Core Concepts: It covers essential topics such as variables, primitive data types ( int ,
double , char ), operators, and expressions.
Control Flow: The material explains how to manage program logic using if-else
statements.
Methods: It details the structure of methods, including parameters, return types, and
overloading.
Data Structures: The guide introduces arrays, covering their declaration, initialization,
and manipulation.
Section 1 Intro ( with JShell )
14 Introduction to JShell
type /help for introduction
/list -all
The up and down arrow keys will be very useful to us, as we execute one line of
code, then want to change something about it, and execute it again.
Up key ~ previous
Down key ~ next
/exit or /ex
Goodbye
Use of Curly braces {
Introduction to Java
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d

Partial preview of the text

Download Java Programming Exercises: Methods and Overloading and more Summaries Programming Languages in PDF only on Docsity!

Introduction to Java

This is a Notion page that created by myself (alge) to teach myself. I thought I might help the newbies.

For practice, visit codewars.com and set the exercise difficulty to 8 KYU. This will train you on the concepts that the page teaches.

Java Fundamentals for Beginners: The guide provides a foundational introduction to Java, starting with the interactive JShell. Core Concepts: It covers essential topics such as variables, primitive data types ( int , double , char ), operators, and expressions. Control Flow: The material explains how to manage program logic using if-else statements. Methods: It details the structure of methods, including parameters, return types, and overloading. Data Structures: The guide introduces arrays, covering their declaration, initialization, and manipulation.

Section 1 Intro ( with JShell )

14 Introduction to JShell

type /help for introduction

/list -all

The up and down arrow keys will be very useful to us, as we execute one line of code, then want to change something about it, and execute it again. Up key ~ previous Down key ~ next

/exit or /ex

Goodbye

Use of Curly braces {

One other useful thing in JShell, is when you want to write a couple lines of code in Java, but you don't want them to be all on the same line. You can do this with the use of curly braces.

The new prompt we are seeing in the JShell terminal is telling us that JShell is ready for another line of code to be typed in.

And you can keep adding new lines of code, pressing Enter in between without anything actually running, until you complete the set of curly braces.

Use of Curly braces {

One other useful thing in JShell, is when you want to write a couple lines of code in Java, but you don't want them to be all on the same line. You can do this with the use of curly braces.

The new prompt we are seeing in the JShell terminal is telling us that JShell is ready for another line of code to be typed in.

And you can keep adding new lines of code, pressing Enter in between without anything actually running, until you complete the set of curly braces.

15 Hello World

JShell let you do it, but a Java compiler and any integrated development environment, wouldn't allow it.

It's important to note that once you declare a variable, you cannot redeclare it in a normal Java code block, even if you're redeclaring it with the exact same data type.

We can assign a value to a variable multiple times in Java, but itʼs the declaration (which includes the data type) that cannot normally be done a second time for the same variable.

public class Example { public static void main(String[] args) { int number  10; // This is the declaration and initialization number  20; // This is a reassignment, which is allowed number  30; // Another reassignment, still allowed // int number  40; // This would cause an error because 'number' is alread y declared in this scope  Error on IDE, not on JShell ) } }

Slides-First-Steps-Variables.pptx

17 Starting out with Expressions

Keywords need to be in lowercase. And variables will always be exactly as you declare them, including capitalization. Remember that case matters in Java code!

Slides-First-Steps-Starting-out-with-Expressions.pptx

18 Primitive Types  Overflow and Underflow)

Whole Number Real Number ( floating point ordecimal )

byte short int long float double Single Character Boolean value char boolean

An integer is a whole number without any fractional or decimal parts. The int data type, like most data types in Java, has a specific range of allowed values. This means there are defined minimum and maximum values—the range isn't infinite. This principle applies to all numeric data types: you cannot assign values outside their specified ranges. These ranges can be checked in code, which is sometimes necessary when testing if a value falls within the allowed limits.

Slides-First-Steps-Primitive-Types.pptx

19 byte, short, long and width

Whole Number Data Type Wrapper Class^ Whatʼs noteworthy byte Byte Has the smallest range short Short

int Integer Javawhole numbersʼs default data type for

long Long Has the largest range

Size of Primitive Types and Width Data Type Width (in bits) Min Value Max Value byte 8  128 127 short 16  32768 32767 int 32  2147483648 2147483647

Long explanation

By default, Java treats whole numbers like 100 as integers (int type). To specify a different data type, you can add a suffix to the number.

For the long data type, you add the suffix 'L'. This is one of the rare cases where Java isn't case-sensitive—both lowercase 'l'

and uppercase 'L' work the same way.

Using the 'L' suffix explicitly tells Java to treat a number as a long type.

Without this suffix, Java treats the number as an int, which can lead to errors if the number exceeds the int type's maximum value.

For this reason, it's best practice to always use the 'L' suffix when working with long numbers.

Slides-First-Steps-byte-short-long-and-width.pptx

20 Casting in Java

This statement works because the result is an int, and assigning it to an int variable is fine.

This statement doesn't work because the expression my min short value divided by two is an int, and an int can't be assigned to a short because the compiler won't guess the result. myMinValue = long

This statement works because the result of -128 divided by 2 is an int. But when calculations use only literal values, the compiler can determine the result immediately and knows the value fits into a short.

This code works, because we tell the compiler we know what we're doing by using this cast, and the compiler doesn't give an error.

Bana short cinsinden byte/2ʼnin değerini yaz. Eğer bana

short yazmazsan byte/2 nin otomatik olarak integer

olduğunu düşünür ( ya da sola kafama göre (integer)

yazısı eklerim ) ve error veririm.

Slides-First-Steps-Casting-in-Java.pptx

21 Primitive Challenge

22 Float and Double Primitives

Double is more precise than float / Double is the default type in floating point numbers.

The double data type can be specified as a numeric literal with a suffix of either lowercase 'd', or uppercase 'D', but because doubles

Why should we choose double? First, it's actually faster to process on many modern computers. That's because computers have, at the chip level, the functionality to actually deal with these double numbers faster than the equivalent float. Second, the Java libraries that we'll get into later in the course, particularly math functions, are often written to process doubles and not floats, and to return the result as a double. The creators of Java selected the double because it's more precise, and it can handle a larger range of numbers.

Slides-First-Steps-Floating-Point-Precision-and-a-Challenge.pptx

24 The char and boolean Primitive Data Types

char String Holds one, and only one, character Can hold multiple characters Literal enclosed in Single Quotes Literal enclosed in Double Quotes

The reason itʼs not just a single byte, is that a char stored as a 2 byte number, similar to the short.

When you print a char, you will see the mapped character, and not the representative number.

Unicode is an international encoding standart for use with different languages and scripts by which each letter, digit, or symbol is assigned a unique numeric value that applies across different platforms and programs.In the English alphabet, weʼve got the

English QWERTY keyboard. ▪ Java uses the Unicode character encoding scheme that supports not only ASCII characters but also all the characters needed for processing the majority of living languages throughout the world. The table below shows both ASCII code and Unicode values of commonly used characters.

https://symbl.cc/en/unicode/blocks/basic-latin/

Size of Primitive Types and Width Characters ASCII Value Unicode Value ‘0ʼ to ‘9ʼ 48 to 57 \u0030 to \u ‘Aʼ to ‘Zʼ 65 to 90 \u0041 to \u005A ‘aʼ to ‘zʼ 97 to 122 \u0061 to \u007A

**Note: ASCII code values in the table are given as decimal (base 10) numbers whereas Unicode values are hexadecimal (base

  1. starting with \u.**

import java.util.Scanner public class CharDemo1  public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a character: "); char ch = input.next().charAt(0); if (ch >= 'A' && ch<= 'Z') { System.out.println(ch + " is an uppercase characte r: "); } else if (ch >= 'a' && ch <= 'z') { System.out.println(ch + " is an lowercase char acter: "); } else if (ch >= '0' && ch <= '9') { System.out.println(ch + " is a numeric charact er: "); } else  System.out.println(ch + " is not an alphanumeric character");} }}

📌 Example Console Inputs and Outputs:

Enter a character: J J is an uppercase letter Enter a character: c c is a lowercase letter Enter a character: 7 7 is a numeric character Enter a character: +

  • is not an alphanumeric character

import java.util.Scanner; public class CharDemo2  public static void main(String[] args) { char ch = 'b'; System.out.println("ch: " + ch); System.out.println("++ch: " + ++ch); System.out.println("ch++: " + ch++); System.out.println("ch: " + ch); System.out.println(" --ch: " + --ch); System.out.println("ch--: " + ch--); System.out.println("ch: " + ch); } }

📌 Console Output of the Program:

ch: b ++ch: c ch++: c ch: d --ch: c ch--: c ch: b

String description 1

Whole number Real Number (floating point or decimal) byte short int long

float double

Single Character Boolean Value char boolean

Aynı string variableʼı üzerine sonradan ekleme yapabilmek mümkün

SYMBOLS

String is immutable which means that you canʼt change a String after itʼs created.

The String ( S is capital for varibles attention! )class is immutable, but can be used much like a primitive data type.

The StringBuilder class is mutable, meaning it can be changed, but does not share the Stringʼs special features, such as being able to assign it a String literal or use the plus operator on it.

Slides-First-Steps-Primitive-Types-Recap-and-the-String-Data-Type  1   1 .pp tx

26 Operators, Operands and Expressions

When an integer variable assigned to another integer variable. The new (previousResult) variable isnʼt going to change after the old variable changes.