Giter Club home page Giter Club logo

java-se's Issues

Literals

Why do we need to differentiate by writing f and d in the end of decimal number, if we had already declared datatypes float/double in the beginning??
like for
float values lies between 3.4028235E38
so decimal value greater than float limit can directly be written in double like this
double x=3.4028235E39 , why do we have to write f and d , if it can easily been differentiate by there limits.

Please help me with my issue.

Problem in SCThread2.java

Sir, In the Interthread communication problem for one producer and multiple consumer there is a problem in the code. I have fixed the problem please review
class WhiteBoard {
boolean flag[] = new boolean[10];
String text;
int numberOfStudents = 0;
int count = 0;

public WhiteBoard() {
initializeFlag();
}

public void initializeFlag() {
for (int i = 0; i < 10; i++) {
flag[i] = false;
}
}

synchronized public void changeFlag(Student s) {
String name = s.name;
int idx = Integer.parseInt(name.substring(0, 1)) - 1; // Adjust index
flag[idx] = true;
}

synchronized public boolean checkFlag(Student s) {
String name = s.name;
int idx = Integer.parseInt(name.substring(0, 1)) - 1; // Adjust index
return flag[idx];
}

public void attendance() {
numberOfStudents++;
}

synchronized public void write(String t) {
System.out.println("Teacher is Writing " + t);
while (count != 0)
try {
wait();
} catch (Exception e) {
}
text = t;
initializeFlag();
count = numberOfStudents;
notifyAll();

}

synchronized public String read() {
while (count == 0)
try {
wait();
} catch (Exception e) {
}

String t = text;
count--;
if (count == 0) {
  
  notify();
}
return t;

}

}

class Teacher extends Thread {
WhiteBoard wb;

String notes[] = { "Java is language", "It is OOPs", "It is Platform Independent", "It supports Thread", "end" };

public Teacher(WhiteBoard w) {
wb = w;
}

public void run() {
for (int i = 0; i < notes.length; i++)
wb.write(notes[i]);
}

}

class Student extends Thread {
String name;
WhiteBoard wb;

public Student(String n, WhiteBoard w) {
name = n;
wb = w;
}

public void run() {
String text = "";
wb.attendance();

do {
  if (!wb.checkFlag(this)) {
    text = wb.read();
    wb.changeFlag(this);
    System.out.println(name + " Reading " + text);
    System.out.flush();

  }

} while (!text.equals("end"));

}

}

public class Main {
public static void main(String[] args) {
WhiteBoard wb = new WhiteBoard();
Teacher t = new Teacher(wb);

Student s1 = new Student("1. John", wb);
Student s2 = new Student("2. Ajay", wb);
Student s3 = new Student("3. Kloob", wb);
Student s4 = new Student("4. Smith", wb);

t.start();

s1.start();
s2.start();
s3.start();
s4.start();

}
}

correct one

import java.lang.*;
class Rectangle
{
private double length;
private double breadth;

public Rectangle()
{
    length=1;
    breadth=1;
}

public Rectangle(double l,double b)
{
    setLength(l);
    setBreadth(b);
}

public Rectangle(double s)
{
    length=breadth=s;
}

public void setLength(double l)
{
    if(l>0)
         length=l;
    else
         length=0;

}
public void setBreadth(double b)
{
    if(b>0)
         breadth=b;
    else
        breadth=0;
}

public double Area()
{
    return length*breadth;
}

public double perimeter()
{
    return 2*(length+breadth);
}

public double circumference()
{
    return perimeter();
}

public boolean isSquare()
{
    if(length==breadth)
        return true;
    else
        return false;
}

}
class DataHidingConstructors
{
public static void main(String argms[])
{
Rectangle r1=new Rectangle(-1,-2);

    System.out.println(r1.Area());
    System.out.println(r1.perimeter());
    System.out.println(r1.circumference());
    System.out.println(r1.isSquare());
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.