0% found this document useful (0 votes)
4 views23 pages

Java Ders4

Uploaded by

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

Java Ders4

Uploaded by

Ahmet Boz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA

PROGRAMLAMA
Dr. Öğr. Üyesi Asiye Tuğba OLGUN ALPYAGIL
İletişim: [Link]@[Link]

Okan Üniversitesi MYO


2024-2025
Sınıf Aktivitesi

Girilen yarıçapa göre kürenin yüzey alanını ve hacmini


hesaplayan Java programı.

Kürenin Yüzey Alanı = 4πr²


Kürenin hacmi = 4/3π r³
Sınıf Aktivitesi
• Program kullanıcıdan iki tam sayı girmesini
ister:
• Bölünecek sayı ve bir bölen

• Daha sonra şunları hesaplar:


• Bölme operatörü (/) kullanılarak bölüm.
• Modül operatörü (%) kullanılarak kalan.
Java Programlamada Operatörler
Bileşik Atama Operatörleri
Örnek
İlişkisel Operatörler
Mantıksal Operatörler
Örnek
Örnek

public static void main(String[] args) {

boolean a = true;
boolean b = false;

// AND (&&)
boolean andSonucu = a && b; // false
[Link]("AND Sonucu: " + andSonucu);

// OR (||) boolean orSonucu = a || b; // true


[Link]("OR Sonucu: " + orSonucu);

// NOT (!) boolean notSonucu = !b; // true


[Link]("NOT Sonucu: " + notSonucu); }
Mantıksal Yapılar

• Bir bilgisayar programının geliştirilmesinde


kullanılan programlama dili ne olursa olsun bu
programların akış şemalarında genel olarak üç basit
mantıksal yapı kullanılır.

1. Sıralı Yapı
2. Karar Verme Yapısı
3. Tekrarlı Yapı
Mantıksal Yapılar: Sıralı Yapı

• Sıralı yapı, hazırlanacak programdaki her işlemin


mantık sırasına göre nerede yer alması gerektiğini
vurgular. Bu yapı sona erinceye kadar ikinci bir
işlem başlayamaz.

Başla
İşlem 1 İşlem 2 İşlem 3
Bitir
Mantıksal Yapılar: Karar Verme Yapısı
• Birden fazla sıralı yapı seçeneğini kapsayan
modüllerde, hangi şartlarda hangi sıralı yapının
seçileceğini belirler.

Girdi

Doğru Yanlış
Koşul

İşlem 1 İşlem 2

Bitir
Mantıksal Yapılar: Tekrarlı Yapı
• Algoritma içinde, bazı satırlar tekrarlı şekilde
işlem görüyorsa, bir döngü söz konusudur.
Döngülere belirli bir koşul geçerli olduğu sürece
devam eden eylemleri tanımlamak için başvurulur.

Başla

Doğru Yanlış
İşlemler Koşul

Bitir
Java’da Kontrol Yapıları
Belirli bir şarta bağlı farklı işlemler yapmak
gerektiğinde kullanılır. Dolayısıyla programın akışını
kontrol eden ifadelerdir. Üçe ayrılmaktadır;

1) If yapısı

2) Switch yapısı

3) ? : operatörü
 if ve if-else yapısı
 if ve if-else yapısı
if (Şart) if (Şart)

{ {

İşlem 1;
İşlem 1;
}
}
else
(if yapısı)
{

İşlem 2;

(if-else yapısı)
Günlük hayatta karşımıza gelen, hayata dair
problemlerimizin çözümünde sıkça kullandığımız bir
yapıdır.

• ‘Bana su getirirsen sana kumandayı veririm’ if yapısına


örnektir. Su getirme şartına bağlı olarak şartın
gerçekleşmesi durumunda işlem yapılacaktır. Şartın
gerçekleşmemesi programı ilgilendirmez.

• ‘Bana su getirirsen kumandayı veririm, aksi halde


elinden telefonumu alırım’ if-else yapısına örnektir.
Su getirme şartına bağlı olarak şartın gerçekleşmesi
veya gerçekleşmemesi durumlarında farklı
davranılacaktır.
Şart ifadesinin akış diyagramı ile
temsili

Evet Hayır
Koşul

İşlem 1 İşlem 2

Bitir

Common questions

Powered by AI

The three basic logical structures used in algorithm design are sequential, decision, and loop structures. The sequential structure executes statements in a specific order until completion, ensuring processes are conducted in the intended sequence without interruptions. Decision structures, like 'if' and 'switch', choose between different paths based on conditions, allowing a program to decide actions based on data. Loop structures manage repetition, executing statements multiple times based on a condition, facilitating repeated tasks efficiently. Together, these structures manage the program flow by determining execution order, decision-making scenarios, and iterative processes.

Designing a Java program for basic arithmetic operations involves defining input variables and implementing methods to perform addition, subtraction, multiplication, and division. Considerations include ensuring correct data types, validating user input to prevent errors like division by zero, and formatting output for readability. Use Scanner for input handling and System.out for output. Error-checking constructs like try-catch can handle exceptions. The program should guide users through operations, ensuring clarity and correctness while maintaining robustness and ease of use.

In Java, a program calculates the division operation using the '/' operator and the modulus operation using the '%' operator. For example, given integers dividend and divisor, the division is calculated as int quotient = dividend / divisor; and the modulus as int remainder = dividend % divisor;. A simple print statement like System.out.println("Quotient: " + quotient + " Remainder: " + remainder); can display these results.

Java uses relational operators such as ==, !=, >, <, >=, and <= to evaluate logical expressions by comparing the values of two operands. An example from the document is the use of logical AND (&&) and OR (||) operators with boolean variables: boolean andSonucu = a && b; results in false because both operands must be true. boolean orSonucu = a || b; results in true because at least one operand is true.

'If' statements in Java execute a block of code based on a true condition, without an alternative path for false conditions. They are used when only one possible action is needed. In contrast, 'if-else' statements provide an alternative code block for false conditions and are used when two distinct paths are needed. For example, 'if' can be used to check a condition like if(salary > 5000) {giveRaise();}. 'If-else' is suitable for situations like if(salary > 5000) {giveRaise();} else {alertUser();}, where an alternative action is needed for both cases.

Understanding both mathematical and Boolean logic is crucial in Java programming as they form the foundation for processing data and making decisions programmatically. Mathematical logic is essential for tasks such as numeric calculations, while Boolean logic is key for decision-making processes, where conditions are evaluated as true or false. Mastery of these concepts allows for robust, efficient programming, enabling tasks ranging from simple calculations to complex algorithmic decision-making. This ability to implement logical operators and mathematical formulas ensures accurate results and functional program flows.

Java's control structures such as 'if-else', 'switch', and loops provide a robust framework for organizing tasks and making logical decisions in algorithms. Their benefits include clear syntax, versatility in handling different scenarios, and the ability to simplify complex decision-making processes. However, they can be limited by complexity and readability issues in deeply nested conditions, and inefficient resource use in some scenarios. Effective use requires balancing clarity and complexity, ensuring that the chosen structures enhance program efficiency and maintainability.

Logical structures in Java programming, such as sequential, decision, and loop structures, define how program operations are organized and execute. Sequential structures execute commands in order; decision structures like 'if' decide based on conditions, and loop structures like 'for', 'while', and 'do-while' repeat operations while certain conditions hold. Implementation involves using language-specific syntax to govern the flow of operations logically, ensuring that operations match the intended program logic.

The surface area of a sphere is calculated using the formula 4πr², and the volume is calculated using the formula 4/3πr³. In a Java program, these can be implemented using basic arithmetic operations with the Math.PI constant for π. The user inputs the radius, and the program computes the surface area with double area = 4 * Math.PI * Math.pow(radius, 2); and the volume with double volume = (4/3.0) * Math.PI * Math.pow(radius, 3)

A 'switch' statement is generally more efficient and readable than 'if-else' when multiple discrete values and one variable are involved because it is optimized for evaluating single expressions against numerous constant values. In contrast, an 'if-else' statement is preferable when evaluating more complex conditions or different variables. The decision is based on the number of conditions, clarity, and the necessity of evaluating numerical or boolean expressions.

You might also like